Question

Im designing an RPG for a class project, working on a gimmicky idea of an item that can be configured to give certain bonuses to existing classes.

But for some reason getting the While loop to loop properly and not repeat the print("invalid choice") for eons AND getting the function itself to work, are beyond me.

Any help is appreciated!!

#PowerTreadFunction
def askPowerTread():
    choice = none
    while choice not in ['A','B','C']:
        print("Invalid choice.")
    choice = input("""Your Power Treads offer the following configurations which each                give distinct bonuses, choose now:\n    
 a) DAMAGE\nb) DEFENSE\nc) ARMOR CLASS\n""")


    if choice == 'A':
        printSlow("Configuring Power Treads for Damage, you now recieve a +5 damage bonus")
        player[DAM] += 5

    elif choice == 'B':
         printSlow("Configuring Power Treads for Defense, you now recieve a +5 defense bonus")
         player[DEF] +=5

    elif choice == 'C':
         printSlow("Configuring Power Treads for Armor Class, you now recieve a +5 armor class bonus")
         player[AC] +=5
Was it helpful?

Solution 2

#PowerTreadFunction
def askPowerTread():
    choice = none
    while choice not in ['A','B','C']:
        print("Invalid choice.")
    choice = input("""Your Power Treads offer the following configurations which each                give distinct bonuses, choose now:\n    
 a) DAMAGE\nb) DEFENSE\nc) ARMOR CLASS\n""")

You assign choice to none (I think you meant None) and then enter the loop without changing it.

choice = input(...) is outside the while loop so it will never execute. Try:

def askPowerTread():
        while True:
            choice = input(...)
            if choice in [...]:
                break
            else:
                print('{choice} is an invalid choice'.format(choice=choice))

To demonstrate the difference between none and None:

>>> type(none)
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    type(none)
NameError: name 'none' is not defined
>>> type(None)
<class 'NoneType'>
>>> 

OTHER TIPS

your problem is the indentation:

def askPowerTread():
    choice = None
    while choice not in ['A','B','C']:
        print("Invalid choice.")
    choice = input(...)

here you're looping over the print statement, but not asking for a new choice to be made, which is outof the while block.

def askPowerTread():
    choice = None
    while choice not in ['A','B','C']:
        print("Invalid choice.")
        choice = input(...)

should solve your problem. Once that's solved, the code you've pasted looks fine to me.

edit: @IanAuld, You're right, to solve this:

PROMPT="""\
 Your Power Treads offer the following configurations which each
 give distinct bonuses, choose now:
  a) DAMAGE
  b) DEFENSE
  c) ARMOR CLASS

"""

def askPowerTread():
    choice = input(PROMPT)
    while choice not in ['A','B','C']:
        print("Invalid choice.")
        choice = input(PROMPT)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top