0

这是 Python 3。如果用户在池中剩余 0 点,并从键值中获取点并将它们添加回池中,则程序将中断。这让我很困惑,因为此时 pool > 0 并且第一个 while 循环应该启动,但它没有。谁能提供任何解释为什么?

PS-一般代码批评也受到赞赏。对此很陌生。

pool = 30

attr = {'Strength' : 0, 'Health' : 0, 'Wisdom' : 0, 'Dexterity' : 0}
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
4

2 回答 2

2

while一旦将它们添加回池中,您就没有循环将您带回到第一个循环。

最简单的解决方法是将print和关联pool == 0的循环放在另一个循环中。

于 2013-10-22T03:15:29.300 回答
1

一旦第一个循环完成pool==0,即代码无法返回到第一个循环。

尝试这个:

while pool >= 0:
    if 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.')



    if pool == 0:
        print('\t\t You have no more points! Add some to the pool!')
        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
于 2013-10-22T03:22:18.707 回答