Python 如何重新启动大循环(内部有2个其他小循环)
所以我有这个程序,它工作(数学上),即使用户可以毫无问题地使用它。唯一的问题是,最终当程序要求用户退出或重新启动时,它不会! 那么我怎样才能重新开始游戏!!! 谢谢大家的帮助Python 如何重新启动大循环(内部有2个其他小循环),python,loops,weak,Python,Loops,Weak,所以我有这个程序,它工作(数学上),即使用户可以毫无问题地使用它。唯一的问题是,最终当程序要求用户退出或重新启动时,它不会! 那么我怎样才能重新开始游戏!!! 谢谢大家的帮助 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
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
print('猜猜我在想什么数字\n')
Max=int(输入('给我们您的间隔可以拥有的高点编号:'))
最小值=0
打印“现在考虑一个介于0和”之间的数字,Max,“一旦您选择了numebr”
“太好了!”
打印“如果数字为:精确(0)、较高(1)或较低(-1),则轮到您帮助我们”
环境=(最大最小)/2
print int(环境),“这是你想的数字吗?”
z=输入(str('只回答0、1或-1:'很重要)'))
x=假
当环境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
看起来您的主程序超出了while循环。您应该将代码放入函数中,并在主循环中调用该函数。另外,在主循环中,您正在测试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()
def game():
打印('猜猜我在想什么数字\n')
Max=int(输入('给我们您的间隔可以拥有的高点编号:'))
最小值=0
打印“现在考虑一个介于0和”之间的数字,Max,“一旦您选择了numebr”
“太好了!”
打印“如果数字为:精确(0)、较高(1)或较低(-1),则轮到您帮助我们”
环境=(最大最小)/2
print int(环境),“这是你想的数字吗?”
z=输入(str('只回答0、1或-1:'很重要)'))
x=假
实际上,我希望如果用户按9,它会从一开始就重新启动游戏,我认为差异很容易找到。他只是把第一个循环打包成一个函数,然后改成第二个循环来调用这个函数。。。我应该像@wcb98说的那样做。唯一的区别也是,虽然在函数中是真的,但您的主要游戏在game()函数中,这使代码更易于阅读