如何创建适当的while循环?

时间:2019-05-29 03:37:36

标签: python-3.x while-loop

我正在尝试创建一个简单的数字猜谜游戏。据我了解,如果用户输入不等于RNG生成的数字,则可以将while循环用作适当的goto函数。包括我创建的代码。我要尝试的是告诉程序如果x<yprint("Too low. Try again"),然后循环返回以要求用户输入。如果x>y,则具有相同的原理。正确猜出数字后,它会显示一条消息,要求输入Y / N,然后关闭程序或循环回到开头。我将如何去做?

我尝试了一个while循环,但是遇到了以下问题:假设RNG生成50。如果我猜是49,它会输出x<y消息,并要求另一个输入,但是即使在新的输入x<yx>y中,它也会保持显示x==y消息。

此外,当正确猜出答案时,我不确定该怎么做,因此如果他们回答Y,程序将重新启动。

最后,当游戏提示用户回答是/否时,由于某种原因,终端将在用户有机会输入之前显示字母“ Y”。

print()
print("Guess an integer between 1-100.")
import random
y=random.randint(1,101)
print(y)
x=int(input())
if x==y:
    print(x, "is correct! Would you like to try again? Y/N")
    if input("Y"):
        print("Too bad! This game isn't finished yet!")
        exit()
    if input("N"):
        print("Good! This game isn't finished yet!")
        exit()
else:
    if x<y:
        print("Too low. Try again.")
    if x>y:
        print("Too high. Try again.")

2 个答案:

答案 0 :(得分:0)

好的,所以我不确定您的if语句和while循环系统如何工作,但是我确实知道您使用的输入函数不正确。

如果要检查输入是否等于a thing,则应该执行:if input('>>> ') == 'a thing'

这将输出>>>,用户可以输入其响应,如果等于a thing,则返回true。

我想我知道这能做什么,但是我不确定。最后,我将包括要做的事情。

if x==y:
    print(x, "is correct! Would you like to try again? Y/N")
    if input("Y"):
        print("Too bad! This game isn't finished yet!")
        exit()
    if input("N"):
        print("Good! This game isn't finished yet!")
        exit()
while True:
    if x<y:
        print("Too low. Try again.")
        int(input())
    if x>y:
        print("Too high. Try again.")
        int(input())

我认为您正在尝试检查用户是否可以一口气猜出来,然后告诉他们在接下来的任何猜想中他们的猜测是太高还是太低。 我会这样:

import random

print('guess a number game (1-100 range)')

y = random.randint(1, 101)

x = int(input('>>> '))

if x == y:
    print('yay, you got it first try')
    exit()
else:
    print('nope')
    while x != y:
        x = int(input('>>> '))
        if x < y:
            print('Too low!')
        elif x > y:
            print('Too high!')
        elif x == y:
            print('E P I C')
            exit()

以下是示例输出:

guess a number game (1-100 range)
>>> 48
nope
>>> 50
Too high!
>>> 25
Too low!
>>> 35
Too low!
>>> 45
Too high!
>>> 40
Too low!
>>> 44
E P I C

我已经有一段时间没来这里了(哈哈),所以这可能不是最好的答案。希望这会有所帮助!

答案 1 :(得分:0)

我认为这是您想要的模式。

import random

EXIT = False

while True:
  if EXIT:
     break
  y=random.randint(1,101)
  print("Random ",y)
  while not EXIT:
    print("Guess an integer between 1-100.")
    x=int(input())
    if x==y:
        print(x, "is correct! Would you like to try again? (Y/N)\n")
        z = input()
        if z == 'Y':
            print("Too bad! This game isn't finished yet!\n")
            break
        if z == 'N':
            print("Good! This game isn't finished yet!\n")
            EXIT = True
            break
    else:
        if x<y:
            print("Too low. Try again.\n")
        if x>y:
            print("Too high. Try again.\n")

输出将是。

Random  89
Guess an integer between 1-100.
20
Too low. Try again.

Guess an integer between 1-100.
100
Too high. Try again.

Guess an integer between 1-100.
89
89 is correct! Would you like to try again? (Y/N)

Y
Too bad! This game isn't finished yet!

Random  36
Guess an integer between 1-100.
36
36 is correct! Would you like to try again? (Y/N)

N
Good! This game isn't finished yet!