2013-10-22 41 views
0

这是Python 3.如果用户在池中剩余0个点,并且已从键值中取得点并将其添加回池中,则程序将中断。这让我感到困惑,因为在这一点上,pool> 0,第一个while循环应该启动,但不是。任何人都可以提供任何解释为什么?代码意外中断

PS-general code critique also appreciate。这是新的。

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 
+0

最有可能的,因为'pool'不是'> 0',但既然你不显示的代码的一部分,没有人会知道。 –

+0

Ach!修复。抱歉。那里。但是当用户在第二个while循环内添加更多点时,它是> 0。 – Zack

回答

2

你没有一个循环,带你回到上升到第while循环,一旦他们重新添加到池中。

最简单的修复方法是将print和相关的pool == 0循环放入另一个循环中。

+0

我该如何去把这段代码连接到第一个while循环? – Zack

+0

@Zack,你需要做的就是缩进它,或者移动到'print'所在的位置。 –

1

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

试试这个:

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