我试图让电脑在我的游戏列表中生成一个数字,但它总是出现故障

2024-04-20 13:41:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在制作我的第一个python项目,它基本上是一个非常简单的数字猜测终端游戏,目前我正在制作3个关卡中的第一个关卡

这个游戏的工作原理是让python在一定的数字范围内生成一个随机数,然后玩游戏的人必须猜出数字,终端会回答“恭喜你得到了正确的数字”或“哦,你得到了错误的数字”

每当我运行程序时,即使我知道我只有一个数字可供选择,终端也会说我得到的数字不正确,尽管我100%地知道它不是真的。我已经尝试了很多不同的方法。我正在考虑改变数字的格式,比如在一个范围内或类似的东西,但它仍然不承认我的数字是正确的

我的代码不是最好的,但我一直在努力让它变得更好,下面是代码:

import random
import numpy
import time

def get_name():
name = input("Before we start, what is your name?")
print("You said your name was: " + name)

# The Variable 'tries' is the indication of how many tries you have left
tries = 1

while tries < 6:

    def try_again(get_number, random, tries):
        # This is to ask the player to try again
        answer = (input(" Do you want to try again?"))

        if answer != "no":
            print("Alright!, well I am going to guess that you want to play again")
            tries = tries + str(1)
            print("You have used up: " + tries + " Of your tries. Remember, when you use 5 tries the game ends")
            get_number(get_name, random, try_again)

    def find_rand_num(get_number, random, tries):

        num_list = [1,1]
        number = random.choice(num_list)

        # Asks the player for the number
        ques = (input("guess your number, since this is the first level you need to choose a number between 1 and 10  "))
        print(ques)

        if input == number:
            print("Congratulations! You got the number correct!")
            try_again(get_number, random, tries)

        elif input != number:
            tries += 1
            print("Oops, you got the number wrong")
            try_again(get_number, random, tries)
        
    def get_number(get_name, random, try_again, tries, find_rand_num):
    
        # This chooses the number that the player will have to guess                
        print("The computer is choosing a random number between 1 and 10... beep beep boop")
        find_rand_num(get_number, random, tries)


    get_name()
    get_number(get_name, random, try_again, tries, find_rand_num)

我最近开始学习编程,因为我爸爸也是一名程序员,我希望有一天能和他一样出色。在号码生成问题之后,我还有一些其他错误。我很想听听你对我如何使这部分工作的想法。感谢您抽出时间,祝您今天休息愉快


1条回答
网友
1楼 · 发布于 2024-04-20 13:41:28

您正在将input()的结果分配给ques,但是没有使用ques。错误在于“如果输入==数字”。这不是在检查上一次输入调用的结果。它检查函数本身(输入)是否等于一个整数,它永远不会等于

你只是想勾选'number==ques'。然而,这也不太管用。输入返回一个字符串值,您试图将其与整数进行比较。您需要将其转换为int,如“if number==int(ques)”

相关问题 更多 >