如何重新启动大循环(其中2个其他小循环)

问题描述:

所以我有这个程序,它的工作原理(数学),即使用户可以使用它没有问题。唯一的问题是,最终当程序要求用户退出或重新启动时,不要这么做! 那么我该如何重新开始游戏! 谢谢大家的帮助如何重新启动大循环(其中2个其他小循环)

print('guess what number im thinking of\n') 
Max=int(input('Give us your hights number that your interval can have:')) 
Min=0 
print'Now think to a number between 0 and',Max,'once you have chose your numebr' 
print'Great !' 
print'its your turn to help us,if the number is :Exacte (0),higher(1)or lower(-1)' 
milieu=(Max-Min)/2 
print int(milieu),'is that the number your thinking of ?' 
z=input(str('its important that you only answer by 0, 1 or -1:')) 
x=False 
while milieu<Max and x==False: 
    if z==0:     
     print('We had guess what number you chosed') 
     x=True 
    while z!=0: 
     if z==1: 
      x=False 
      Min=milieu 
      milieu=milieu+((Max-Min)/2) 
      print(int(milieu)) 
      z=input('its important that you only answer by E, G or P:') 
     if z==-1: 
      x=False 
      Max=milieu 
      milieu=(milieu-((Max-Min)/2)) 
      print(int(milieu)) 
      z=input('its important that you only answer by E, G or P:') 
      break 
while x==True: 
    a=input('[5] to go out game\nor [9] to restart:') 
    if a==5: break 
    print ("restart") 
    if a==9: 
     x=False 

1)你都可以从输入函数的字符串,你应该把它转换为int。

2)你应该用一个函数来实现自己的目标

def game(): 
    ... # all the code for your game that you posted 

if __name__ == "__main__": 
    while True: 
    game() 
    response = int(raw_input('[5] to go out game\nor [9] to restart:')) 
    if response != 9: 
     break 
+0

其实我想,如果用户按9,它会重新启动游戏(开始) – 2014-09-29 00:15:11

+0

你的超级真棒! It worked :) 谢谢 – 2014-09-29 00:25:59

它看起来像你的主要程序是不可能的,而循环。你应该把你的代码放在一个函数中,并在主循环中调用它。另外,在你的主循环中,你正在测试int和字符串。请记住“5”!= 5。你可以直接将它转换为一个int,或者只是测试它为“5”而不是5.我更愿意测试它为5,所以如果用户输入非数字位数,不会伤害你的程序。

def game(): 
    print('guess what number im thinking of\n') 
    Max=int(input('Give us your hights number that your interval can have:')) 
    Min=0 
    print'Now think to a number between 0 and',Max,'once you have chose your numebr' 
    print'Great !' 
    print'its your turn to help us,if the number is :Exacte (0),higher(1)or lower(-1)' 
    milieu=(Max-Min)/2 
    print int(milieu),'is that the number your thinking of ?' 
    z=input(str('its important that you only answer by 0, 1 or -1:')) 
    x=False 
    while milieu<Max and x==False: 
     if z==0:     
      print('We had guess what number you chosed') 
      x=True 
     while z!=0: 
      if z==1: 
       x=False 
       Min=milieu 
       milieu=milieu+((Max-Min)/2) 
       print(int(milieu)) 
       z=input('its important that you only answer by E, G or P:') 
      if z==-1: 
       x=False 
       Max=milieu 
       milieu=(milieu-((Max-Min)/2)) 
       print(int(milieu)) 
       z=input('its important that you only answer by E, G or P:') 
       break 

while True: 
    a=input('[5] to go out game\nor [9] to restart:') 
    if a=="5": 
     break 
    if a=="9": 
     print("restart") 
     game() 
+0

我认为差异很容易找到。他只是将第一个循环打包到一个函数中,并改为第二个循环来调用该函数。 – Grochni 2014-09-29 00:19:03

+0

所以如果我得到它......我应该像@ wcb98所说的那样做。唯一的区别是,但功能 – 2014-09-29 00:23:25

+0

是真正的,是的,你的主要游戏是在game()函数内部,这使得代码更容易阅读 – wcb98 2014-09-29 00:57:58