Home > Software engineering >  Not able to break out of a while loop unless entered twice, Python
Not able to break out of a while loop unless entered twice, Python

Time:06-15

I have the following Python Script:

def spell_count():
    for key, value in dnd_attributes.CharSpells.items():
        print(key, sep='\n')
    new_spell_dict = dict((k.lower(), v) for k, v in dnd_attributes.CharSpells.items())
    while True:
        spell_input = input("Which spell would you like to use? Would you like to see the Values? Or would you like "
                            "to reset/go back? ").lower()
        if spell_input in new_spell_dict:
            print(new_spell_dict[spell_input])
            if new_spell_dict[spell_input] == 0:
                print("Out of spells for this level")
            else:
                new_spell_dict[spell_input] -= 1
                print(new_spell_dict[spell_input])
        elif spell_input == 'values':
            for key, value in new_spell_dict.items():
                print(key   ': ', value)
        elif spell_input == 'reset':
            new_spell_dict = dict((k.lower(), v) for k, v in dnd_attributes.CharSpells.items())
        elif spell_input == 'back':
            print("Front Page!!")


spell_count()

The dnd_attributes.CharSpells is another python file that contains the following list:

CharSpells = {
    'Level 1': 2,
    'Level 2': 0,
    'Level 3': 0,
    'Level 4': 0,
    'Level 5': 0,
    'Level 6': 0,
    'Level 7': 0,
    'Level 8': 0,
    'Level 9': 0,
}

This currently works if you iterate through it and does exactly what its supposed too. The only way to get out of the loop is if you enter in a prompt such as clear then it will ask you the question again for a prompt and after you enter clear again then it will exit the loop and end the code.

I've tried to rewrite the code in a different way, but couldn't find another way that would work for what I'm trying to do. Essentially, the code is supposed to list out the levels that a user can choose from, and enter level 1 for example. Then it subtracts 1 from that value, if the value reaches 0 then it responds Out of spells for this level if the user enters values then it lists all the levels and the current values of each.

If the user resets then it restarts the values from scratch. If the user enters back then it goes back to another function, but for the time being it's just Front Page!!. If the user enters anything else, then it should back out of the function. However, it doesn't escape out of the function on the first try and needs the user needs to escape out a second time in order to stop the program.

Any help would be greatly appreciated!!

CodePudding user response:

From what I can see the issue here is that your while loop in not conditional. Ie. there is no condition set to force the loop to exit at an appropriate time.

For Example:

x=0
while x < 10:
    #do thing
    x = x   1 #to increment x. This will run through 9 (10?) times and then exit the loop.

Obviously the condition should be changed to suit your use case (eg, limit number of spells to 5 or something).

Or you can use break statements where appropriate in the while loop.

CodePudding user response:

A while True: loop runs forever until you explicitly force the loop to end. This can be done with break statements

For example.

def spell_count():
    for key, value in dnd_attributes.CharSpells.items():
        print(key, sep='\n')
    new_spell_dict = dict((k.lower(), v) for k, v in dnd_attributes.CharSpells.items())
    while True:
        spell_input = input("Which spell would you like to use? Would you like to see the Values? Or would you like "
                            "to reset/go back? ").lower()
        if spell_input in new_spell_dict:
            print(new_spell_dict[spell_input])
            if new_spell_dict[spell_input] == 0:
                print("Out of spells for this level")
                break    # here
            else:
                new_spell_dict[spell_input] -= 1
                print(new_spell_dict[spell_input])
        elif spell_input == 'values':
            for key, value in new_spell_dict.items():
                print(key   ': ', value)
        elif spell_input == 'reset':
            new_spell_dict = dict((k.lower(), v) for k, v in dnd_attributes.CharSpells.items())
        elif spell_input == 'back':
            print("Front Page!!")
            break  # here
        else:
            break


spell_count()
  • Related