2013-10-22 46 views
-3

所以我在这里失去了一些东西。当用户花费了他们所有的积分,并提示他们是否想向池中添加更多点时,并且用户选择'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 
+0

看起来像你的一些'while'循环应if'语句或省略 – cmd

+0

@cmd:由于'print'('不是有效的输入',请更正'')'或'print('请进行有效的选择')'while'路径 –

回答

3

break没有缩进是在elif语句(或在内部while循环):

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

    break 

break前面添加两个标签。

让你的程序可以打破内部循环结束后,你也可能要改变

while True: 

while points > 0: 
+0

感谢您的答复 - 显然它的想法是一个愚蠢的问题。我只是一个初学者。我已经尝试过这个,并且由于某种原因,用户仍然会被提示添加更多点数到池中。 – Zack

+0

@Zack:这是因为你的'break'语句只是在while循环中打破,而不是外部循环。将'true'这一行改为''while points> 0',你就可以解决这个问题。你还必须在print('现在有',pool''剩余'')之后添加一个'break'行,以便你摆脱这个循环。 –