简体   繁体   English

Python - 使用while循环编码随机数猜谜游戏的问题

[英]Python - Problem coding random number guessing game using while loop

first time poster.第一次海报。 I'm having a problem getting my random number guessing game in Python to work properly.我无法让我在 Python 中的随机数猜谜游戏正常工作。 I've purposely set the number to not be random (it's 50) for testing purposes.出于测试目的,我特意将数字设置为非随机数(为 50)。 If I guess a number under 50, it will tell me that the number is higher;如果我猜一个低于 50 的数字,它会告诉我这个数字更高; I can then guess higher than 50 and it will tell me lower.然后我可以猜测高于 50,它会告诉我更低。

For some reason though, if I guess above 50 and then try to guess under 50, the program stops.但是由于某种原因,如果我猜测高于 50,然后尝试猜测低于 50,程序就会停止。

Can someone point me in the right direction please?有人可以指出我正确的方向吗? Here is my code:这是我的代码:

from random import randint
randomNum = 50 #randint(0,100)
print("Let's play a guessing game.  I'm thinking of a number between 0-100. Take a guess 
what it is") 
num = int(input()) 
if(num == randomNum):
    print("Wow, what fantastic luck!  You're right!")
while (randomNum > num):
    print('Its higher than that. Guess again!')
    num = int(input())
    while (num == randomNum):
        print("You got it!")
        break

while (randomNum < num):
    print('Its lower than that! Guess again!')
    num = int(input())
    while (num == randomNum):
        print("You got it!")
        break

I see the issue, when you guess first, you enter your first while loop我看到了问题,当你先猜时,你进入你的第一个 while 循环

while (randomNum > num):
    print('Its higher than that. Guess again!')
    num = int(input())
    while (num == randomNum):
        print("You got it!")
        break

then you guess again with that second input, but if your number isnt higher than 50, then that while loop is over, and the program ends, wrapping th whole thing in a while loop and putting your input at the top like this:然后你用第二个输入再次猜测,但如果你的数字不高于 50,那么 while 循环结束,程序结束,将整个事情包装在一个 while 循环中,并将你的输入放在顶部,如下所示:

num ='anything thats not an int'
while (num != randomnum):
    num = int(input())
    
    if randomNum > num:
        print('Its higher than that. Guess again!')

    elif randomNum < num:
        print('Its lower than that! Guess again!')
print('you guessed the number!')

the last print statement is outside the loop cause the loop will only break once num = randomnum最后一个打印语句在循环之外,因为循环只会中断一次 num = randomnum

im coming from CodeGolf so excuse my godawful readability, ill try my best to explain it.我来自 CodeGolf,所以请原谅我糟糕的可读性,我会尽力解释它。

while 1:
 try:n=hash("1")%int(input("Level: "))+1;break
 except:1
i=""
while i!=n:
 try:i=int(input("Guess: "))
 except:continue
 print("Too large!"if i>n else"Too small!"if i<n else"Just right!")

this part here:这部分在这里:

while 1:
 try:n=hash("1")%int(input("Level: "))+1;break
 except:1

is a while loop that detects a valid number (which is the highest number n can be), and creates a random number from it:是一个 while 循环,它检测一个有效数字(这是 n 可以是最大的数字),并从中创建一个随机数:

while True: try to turn the input into an int then break out the loop, and if it fails, try ask for input again while True:尝试将输入转换为int然后打破循环,如果失败,再次尝试请求输入

ill explain this part next:接下来解释一下这部分:

i=""
while i!=n:
 try:i=int(input("Guess: "))
 except:continue
 print("Too large!"if i>n else"Too small!"if i<n else"Just right!")

make a variable called i (input) and set it to nothing, create a while loop with the condition being i not equaling n (the random number between the user specified range) next, try get input.创建一个名为 i(输入)的变量并将其设置为空,然后创建一个条件为 i 不等于 n(用户指定范围之间的随机数)的 while 循环,尝试获取输入。 the try/except loop is to ensure a number is being supplied (and not a string. if a string was supplied it would give an error, and retry the input prompt until a number is given) finally, is the print, if the input is lower than n, it prints loo low and vise versa, and if the input is equal to the random number, it will print just right, and the while loop will be fulfilled, ending the program! try/except 循环是为了确保提供了一个数字(而不是一个字符串。如果提供了一个字符串,它将给出一个错误,并重试输入提示,直到给出一个数字)最后,如果输入小于n,则打印loo low,反之亦然,如果输入等于随机数,则打印正好,执行while循环,结束程序!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM