2014-09-29 38 views
-1

所以我有這個程序,它的工作原理(數學),即使用戶可以使用它沒有問題。唯一的問題是,最終當程序要求用戶退出或重新啓動時,不要這麼做! 那麼我該如何重新開始遊戲! 謝謝大家的幫助如何重新啓動大循環(其中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 

回答

2

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

-1

它看起來像你的主要程序是不可能的,而循環。你應該把你的代碼放在一個函數中,並在主循環中調用它。另外,在你的主循環中,你正在測試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