2017-07-16 183 views
0

我試圖編寫代碼來解決一個問題:是否有可能在「while」循環中更改變量的值?

生成之間1和9(包括1和9)的隨機數。要求用戶猜出這個數字,然後告訴他們他們是否猜測得太低,太高,或者是否正確。跟蹤用戶採取了多少猜測,並在遊戲結束時打印出來。

,我寫的代碼是:

import sys 
import random 
x=random.randint(1,9) 

print('Hello there! Please enter a number between 1 and 9 including the extremes.') 

for i in range (10): 
    z=input() 
    if int(z)<x: 
     print('Too low. Please try again.') 

    elif int(z)>x: 
     print('Too high. Please try again.') 

    elif int(z)==x: 
     print('You guessed it right!') 

    if i==0: 
     print('It took you a single turn! Nice') 
    else: 
     print('it took you ' + str(i+1)+' turns.') 

    print('Do you want to play again? Yes or No?') 

    j=input() 
    if j.lower()=='yes': 
     print('Okay, Please enter a number between 1 and 9 including the extremes.') 
     pass 
    else: 
     sys.exit() 

這裏是它看起來運行時,如:

Hello there! Please enter a number between 1 and 9 including the extremes. 
4 
Too high. Please try again. 
3 
Too high. Please try again. 
2 
You guessed it right! 
it took you 3 turns. 
Do you want to play again? Yes or No? 
yes 
Okay, Please enter a number between 1 and 9 including the extremes. 
6 
Too high. Please try again. 
4 
Too high. Please try again. 
2 
You guessed it right! 
it took you 6 turns. 
Do you want to play again? Yes or No? 

看到,代碼給出了完美的效果首先執行for循環時。當我們嘗試第二次運行這個「遊戲」時,它會給出奇怪的結果,通過說yes當它問我們問題:Do you want to play again? Yes or No?

是否有可能把i=0當Python到達4號最後一行與for循環從i=0再次啓動,這樣我沒有得到怪異的結果?

或者還有其他更簡單的方法刪除此錯誤嗎?

+0

使用while循環,而不是爲我的range(10)。然後重置計數 –

回答

1

您可以使用while循環執行任務。你應該添加異常處理方法來獲取輸入。

import random 

cond = True 

while cond: 
    print('Hello there! Please enter a number between 1 and 9 including the extremes.') 
    x=random.randint(1,9) 
    for i in range (10): 
     z=int(input()) 
     if int(z)<x: 
      print('Too low. Please try again.') 

     elif int(z)>x: 
      print('Too high. Please try again.') 

     elif int(z)==x: 
      print('You guessed it right!') 
      import sys 
      if i==0: 
       print('It took you a single turn! Nice') 
      else: 
       print('it took you ' + str(i+1)+' turns.') 

      print('Do you want to play again? Yes or No?') 
      j=input() 
      if j.lower()=='yes': 
       break 
      else: 
       cond = False 
       sys.exit() 
+1

感謝您的回答!你能告訴我,爲什麼你在最後一行放了'cond = False'?代碼運行良好,沒有它! –

+0

由於'''sys.exit()'''語句程序將退出。 – Bodhi94

0

整個代碼嵌入在for循環中,計數器永遠不會重置。如果要在for循環中重置i,則必須在for循環之外定義它,以便它具有全局範圍。

+0

這是行不通的。即使我將i聲明爲全局變量,只要我進入for循環,它就會被更改。 –

1

首先,你只選擇一次隨機數,所以它總是相同的。其次,你的遊戲應該在while循環中而不是for循環中(如果你想讓玩家在猜中後重新啓動)。

turns = 0 
while True: 
    secret_number = random.randint(1,9) 
    print('Please enter a number between 1 and 9 including the extremes.') 
    guess = input() 
    turns += 1 
    if int(guess) < secret_number: 
     print("Too low") 
    elif int(guess) > secret_number: 
     print("Too high") 
    else: 
     print("You guessed in {} turn(s)".format(turns)) 

您繼續循環,如果用戶想繼續玩輪流分配= 0,或者你分手,如果他沒有。

1

我會這樣寫,可能。

from itertools import count 
from random import randint 


def run_game(): 
    random_value = randint(1, 9) 
    print('Hello there! Please enter a number between 1 and 9 including the extremes.') 

    for i in count(): 
     guess_string = input() 

     try: 
      guess = int(guess_string) 
     except ValueError: 
      print("Invalid value given for guess: {}".format(guess_string)) 

     if guess < random_value: 
      print("Too low! Please try again.") 

     elif guess > random_value: 
      print("Too high! Please try again.") 

     else: 
      print('You guessed it right!') 

      if not i: 
       print('It took you a single turn! Nice') 
      else: 
       print('it took you {} turns.'.format(i + 1)) 

      print('Do you want to play again? Yes or No?') 

      response_string = input() 
      return response_string.lower() == 'yes' 


if __name__ == "__main__": 
    while run_game(): 
     pass 

但是,對於理解簡單:

from itertools import count 
from random import randint 

if __name__ == "__main__": 
    playing = True 
    while playing: 
     random_value = randint(1, 9) 
     print('Hello there! Please enter a number between 1 and 9 including the extremes.') 

     for i in count(): 
      guess_string = input() 

      try: 
       guess = int(guess_string) 
      except ValueError: 
       print("Invalid value given for guess: {}".format(guess_string)) 

      if guess < random_value: 
       print("Too low! Please try again.") 

      elif guess > random_value: 
       print("Too high! Please try again.") 

      else: 
       print('You guessed it right!') 

       if not i: 
        print('It took you a single turn! Nice') 
       else: 
        print('it took you {} turns.'.format(i + 1)) 

       print('Do you want to play again? Yes or No?') 

       response_string = input() 
       if response_string.lower() != 'yes': 
        playing = False 
       break 
1

所有進口應該在文件的頂部。然後,放一段時間,讓玩家在每場比賽後重新開始;這樣,變量x在每場比賽之後也被重置。此外,第一個打印應該放在while和for循環之外,所以它只打印一次(最後一個if會在新遊戲開始時打印一個新提示)。
你在這一點上的代碼應該是這樣的:

import random 
import sys 
print('Hello there! Please enter a number between 1 and 9 including the extremes.') 
while True: 
    x=random.randint(1,9) 
    for i in range (10): 
     z=input() 
     if int(z)<x: 
      print('Too low. Please try again.') 
     elif int(z)>x: 
      print('Too high. Please try again.') 
     elif int(z)==x: 
      print('You guessed it right!') 
      if i==0: 
       print('It took you a single turn! Nice') 
      else: 
       print('it took you ' + str(i+1)+' turns.') 
      print('Do you want to play again? Yes or No?') 
      j=input() 
      if j.lower()=='yes': 
       print('Okay, Please enter a number between 1 and 9 including the extremes.') 
      else: 
       sys.exit() 
相關問題