不确定为什么循环不会破坏

时间:2013-10-22 17:57:20

标签: python python-3.x while-loop break

所以我在这里遗漏了一些东西。当用户已经花费了他们所拥有的所有点并且被提示他们是否想要向池中添加更多点,并且用户选择“n”时,他们会不断地提示他们向池中添加更多点。哪个不应该发生。我试图摆弄休息时间,但无济于事。当用户输入'n'时,整个循环应该停止。任何帮助,将不胜感激。谢谢。

# Char Creator- pool of 30 pts, spend on Str, Health, Wisdom, Dex
# Spend points in pool on attr, take points and put them back into pool

pool = 30

attr = {'Strength' : 0, 'Health' : 0, 'Wisdom' : 0, 'Dexterity' : 0}

while True:
    while pool > 0:
        print('\t\t Time to spend some points! You have', pool, 'to spend')
        spend = input('Where would you like to spend your points?')
        if spend in attr:
            amount = int(input('How many points would you like to spend?'))
            if amount > pool:
                print('You do not have that many points! You have', pool, 'points!')
            else:
                attr[spend] += amount
                pool -= amount
                print(attr)
        else:
            print('Not a valid input. Please correct.')

    print('\t\t You have no more points! Add some to the pool!')

    while pool == 0:
        confirm = input('Would you like to add points back into the pool?')
        if confirm == 'y':
           take =  input('Where would you like to take points from?')
           if take in attr:
                number = int(input('How many points would you like to take?'))
                if attr[take] >= number:
                    pool += number
                    attr[take] -= number
                    print ('There are now', pool, 'points remaining') 
                elif attr[take] < number:
                    print('You dont have enough points to do that! You have', attr[take], 'points in', take)
           else:
               print('Please make a valid selection.')
        elif confirm == 'n':
            print('Goodbye!')

    break

1 个答案:

答案 0 :(得分:3)

您的break不会缩进到elif语句中(或内部while循环中):

        elif confirm == 'n':
            print('Goodbye!')

    break

break前面再添加两个标签。

为了让你的程序在打破内循环后结束,你也可能想要改变

while True:

while points > 0: