2013-10-22 73 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