2017-10-10 20 views
0

我創建在用戶已經猜到1和10我的代碼將不會選擇一個新的隨機數

之間的隨機選擇號碼我的代碼工作直至點一個小型項目,其中用戶猜測正確的號碼。它會回到「重新啓動= 1」,但不會生成新的號碼?

下面是我的意思是 - https://gyazo.com/1b6c9afc17997e7a0ee059a8b0eeb89e 的屏幕截圖正如你在這種情況下看到的那樣,數字不會從6變爲?

這將是真棒,有任何幫助可能!

# Useful module for selecting random numbers 
import random 

# Return to this point when user guesses the correct number 
restart = 1 

while (restart < 10): 
    # Variable that chooses a random number 
    Random_Number = (random.choice([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])) 

    # Loop back to this point 
    loop = 1 

    # Checks if users number is correct or not 
    while (loop < 10): 

     personStr = input("Guess the random number from 1 - 10?: ") 
     person = int(personStr) 

     if person == Random_Number: 
      print ("You are correct!, the number is", Random_Number) 
      print ("Try to guess the new number!") 
      restart = restart + 1 

     elif Random_Number < person: 
      print ("Your number is smaller than your selected number, try again!") 
      loop = loop + 1 

     else: 
      print ("Your number is larger than your selected number, try again!") 
      loop = loop + 1 
+0

玩家猜出數字後,你並沒有退出內部循環。只需在增加重新啓動後添加一箇中斷。 –

回答

1

添加破退出循環。

# Useful module for selecting random numbers 
import random 

# Return to this point when user guesses the correct number 
restart = 1 

while (restart < 10): 
    # Variable that chooses a random number 
    Random_Number = (random.choice([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])) 

    # Loop back to this point 
    loop = 1 

    # Checks if users number is correct or not 
    while (loop < 10): 

     personStr = input("Guess the random number from 1 - 10?: ") 
     person = int(personStr) 

     if person == Random_Number: 
      print ("You are correct!, the number is", Random_Number) 
      print ("Try to guess the new number!") 
      restart = restart + 1 
      break # add a break here 

     elif Random_Number < person: 
      print ("Your number is smaller than your selected number, try again!") 
      loop = loop + 1 

     else: 
      print ("Your number is larger than your selected number, try again!") 
      loop = loop + 1 
+0

這完美的作品!謝謝你! – ChalzZy

-1

聽起來你正在尋找random.shuffle然後使用環路索引,就像這樣:

# Useful module for selecting random numbers 
import random 

# Return to this point when user guesses the correct number 
restart = 0 

rand_nums = random.shuffle(range(1, 11)). 
# ^Assuming you only want one of each in range (1,11) 

while restart < 10: 
    restart += 1 
    # Loop back to this point 
    loop = 1 

    # Checks if users number is correct or not 
    while loop < 10: 
     loop += 1 

     personStr = input("Guess the random number from 1 - 10?: ") 
     person = int(personStr) 

     if person == rand_nums[restart]: 
      print ("You are correct!, the number is", rand_nums[restart]) 
      print ("Try to guess the new number!") 
      break 
     elif rand_nums[restart] < person: 
      print ("Your number is smaller than your selected number, try again!") 
     else: 
      print ("Your number is larger than your selected number, try again!") 

編輯:正如其他人所說的,你可能會想break你的條件句。上面的代碼現在也包含了這一點。

+0

謝謝你的回答!但是現在代碼已經破裂?這是我現在得到的錯誤? - https://gyazo.com/16294bbbcb6c20d8f4643bf77cb6076e這就是我改變 - https://gyazo.com/8e9df64152e0692605e7f83df93217e2 – ChalzZy

+0

@ChalzZy我已經發布完整的代碼在上下文中。 –

1

您必須添加break爲第二循環:

if person == Random_Number: 
    print ("You are correct!, the number is", Random_Number) 
    print ("Try to guess the new number!") 
    restart = restart + 1 
    break 
0

使用Random_Number = random.randint(1, 10)從1生成隨機整數10,你的條件語句後使用break;

相關問題