使用python循环程序进行项目

时间:2019-04-11 03:51:12

标签: python

我儿子有这个项目,他必须在python中执行该项目,并且被卡住了。

他需要做一个猜数字游戏。该代码必须生成一个介于0到10之间的随机秘密数字,然后给用户5次尝试猜测该数字的机会,如果每次猜测不正确,则必须指出该数字是否高于或低于秘密随机数字。每次猜测之后,代码都需要显示说明发生了什么的文本。该代码还需要存储所有猜测并在最后显示它们。需要使用循环,if,elif,else和数组或列表代码进行构建。

到目前为止的尝试在下面

print("Hi there, lets play a little guessing game. Guess the number between 0 and 10")
from random import randint
x = [randint(0,10)]
counter = 0

guess = input("Enter guess:")

while counter < 5:
    print("You have " + str(counter) + " guesses left")
    counter = counter +1

    if guess == x:
        print("Congrats you got it")
        break
    elif guess > x:
        print("Too high")
    elif guess < x:
        print("Too low")
    else:
        print("You lost")
        break

任何纠正我儿子代码的帮助将不胜感激,因为该项目即将到期,他无法访问他的导师

3 个答案:

答案 0 :(得分:0)

这是更正。因此x已被初始化为数组而不是整数。因此,所有与猜测的比较都不会起作用。同样,计数器逻辑是错误的。不是从零开始,而是从5(这是最大机会)开始,然后从相反的方向开始。然后在每个if / elif循环中附加所有猜测并最终打印出来。

这是更正的代码

from random import randint
x = randint(0,10)
print(x)
counter = 5
guesses=[] #initalize an empty list to store all guesses

while counter != 0:
    guess = input("Enter guess:")

    if guess == x:
        print("Congrats you got it")
        guesses.append(guess)
        break
    elif guess > x:
        print("Too high")
        guesses.append(guess)
    elif guess < x:
        print("Too low")
        guesses.append(guess)
    else:
        print("You lost")
        break

    counter = counter-1
    print("You have " + str(counter) + " guesses left")

print(guesses)

答案 1 :(得分:0)

这应该做到。下面的注释中说明了代码的作用。
您需要执行x=randint(0,10),它将随机数分配给变量,即x=4而不是`x = [randint(0,10)] , which assigns the random number to a list , x = [4]'' `

此外,您还需要在循环中进行猜测,而不是仅在循环开始之前进行一次猜测。 另外,您需要将字符串转换为int以进行比较,例如guess = int(input("Enter guess:"))

print("Hi there, lets play a little guessing game. Guess the number between 0 and 10")
#Create a random number
from random import randint
x = randint(0, 10)
counter = 0
won = False

#Run 5 attempts in a loop
while counter<5:
    #Get the guess from the user
    guess = int(input("Enter guess:"))

    counter = counter+1
    #Check if the guess is the same, low or high as the random number
    if guess == x:
        print("Congrats you got it")
        won = True
        break
    elif guess > x:
        print("Too high")
    elif guess < x:
        print("Too low")
    print("You have " + str(5 - counter) + " guesses left")

#If you didn't won, you lost
if not won:
    print("The number was ", x)
    print("You Lost")

答案 2 :(得分:-1)

编辑: x = [randint(0,10)]在您在此处创建列表而不是单次猜测时不起作用 print("You have " + str(counter) + " guesses left")也不正确。您可以改为将counter设置为5并检查counter> 0并执行counter-= 1,这样可以固定消息 最后,要存储所有猜测,您将需要一个变量

from random import randint

if __name__ == "__main__":
    number_to_guess = randint(0,10)
    guesses = []
    for c in range(5,0,-1):
        guessed = input("Enter guess:")
        guessed = guessed.strip()
        assert guessed.isnumeric()
        guessed = int(guessed)
        guesses.append(guessed) 
        if guessed == number_to_guess:
            print("yes")
            break
        elif guessed > number_to_guess:
            print("more")
        else:
            print("less")            
        c -= 1
        print("pending guesses", c)
    print("Expected - ", number_to_guess)
    print("All guesses - ", guesses)