使用python循环程序的项目

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用python循环程序的项目相关的知识,希望对你有一定的参考价值。

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

他需要做一个猜数游戏。代码必须生成0到10之间的随机密码,然后让用户5尝试猜测该数字,如果不正确则每个猜测必须指示它是否高于或低于秘密随机数。在每次猜测之后,代码需要显示文本,说明发生了什么。代码还需要存储所有猜测并在最后显示它们。需要使用loop,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

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

答案

这应该做到这一点。代码的作用在下面的评论中解释。 你需要做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")
另一答案

所以这里是更正。所以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)

另一答案

编辑:x = [randint(0,10)]不会工作,因为你在这里创建一个列表而不是单一猜测print("You have " + str(counter) + " guesses left")也是不正确的。您可以改为将计数器设置为5并检查计数器> 0并执行计数器 - = 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)

以上是关于使用python循环程序的项目的主要内容,如果未能解决你的问题,请参考以下文章

使用python循环程序的项目

python 用于数据探索的Python代码片段(例如,在数据科学项目中)

循环通过 python 正则表达式匹配

使用循环片段依赖关系模块化单活动Android应用程序

微信小程序代码片段

Python之如何优雅的重试