2017-10-10 93 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;