Python 我的基本';猜数字程序';,它不会承认';对计算机的正确猜测';s编号';

Python 我的基本';猜数字程序';,它不会承认';对计算机的正确猜测';s编号';,python,python-3.4,Python,Python 3.4,我做了一个简单的猜测数字的程序,在我探索学习python的过程中!哈哈 但是,计算机不会承认用户的猜测是正确的,并且它将始终转到“不正确”位置,即使计算机编号显示为用户的猜测。我的代码似乎正确,所以我不知所措。感谢您的帮助 def randomNumGen(): userGuess = input('The Computer has thought of a number between 1-5, deposit your guess now!: ') if finalV

我做了一个简单的猜测数字的程序,在我探索学习python的过程中!哈哈

但是,计算机不会承认用户的猜测是正确的,并且它将始终转到“不正确”位置,即使计算机编号显示为用户的猜测。我的代码似乎正确,所以我不知所措。感谢您的帮助

def randomNumGen():
    userGuess = input('The Computer has thought of a number between 1-5, deposit    your guess now!: ')

    if finalValue == userGuess:
        print("You've guessed correctly!, Very lucky you are!")

    else:
        print("You're guess was incorrect, try again please")
        print("The computer's guess was",finalValue)
        main()
编辑:

输出如下所示,这解释了这里的明显问题:

>The Computer has thought of a number between 1-5, deposit your guess now!: 2
>You're guess was was incorrect 
>The computer's guess was 2

因此,当给出正确的猜测时,“计算机”将无法识别。

最有可能的是
finalValue
是一个整数,而
input()
的输入将是一个字符串。在比较它们之前,应该将两者都转换为字符串,或者将两者转换为整数

要将整数转换为字符串,只需执行以下操作:

my_string = str(my_integer)
这将获取一个名为
my\u integer
的整数,并返回一个字符串,该字符串将被指定为
my\u string

def randomNumGen():
    userGuess = input('The Computer has thought of a number between 1-5, deposit    your guess now!: ')

    if str(finalValue) == userGuess:
        print("You've guessed correctly!, Very lucky you are!")

    else:
        print("You're guess was incorrect, try again please")
        print("The computer's guess was",finalValue)
        main()
这是我所能看到的最简单的改变,它将使这项工作起作用。请注意这一行:

if str(finalValue) == userGuess:

我将
finalValue
转换成字符串与
userGuess

比较,finalValue很可能是一个整数,而
input()
的输入将是一个字符串。在比较它们之前,应该将两者都转换为字符串,或者将两者转换为整数

要将整数转换为字符串,只需执行以下操作:

my_string = str(my_integer)
这将获取一个名为
my\u integer
的整数,并返回一个字符串,该字符串将被指定为
my\u string

def randomNumGen():
    userGuess = input('The Computer has thought of a number between 1-5, deposit    your guess now!: ')

    if str(finalValue) == userGuess:
        print("You've guessed correctly!, Very lucky you are!")

    else:
        print("You're guess was incorrect, try again please")
        print("The computer's guess was",finalValue)
        main()
这是我所能看到的最简单的改变,它将使这项工作起作用。请注意这一行:

if str(finalValue) == userGuess:

我将
finalValue
转换为字符串与
userGuess

进行比较,我认为问题在于,当需要将输入识别为整数时,您的输入被处理为字符串。试试这个:

def randomNumGen():
userGuess = input('The Computer has thought of a number between 1-5,     deposit    your guess now!: ')
userGuess = int(userGuess)

if finalValue == userGuess:
    print("You've guessed correctly!, Very lucky you are!")

else:
    print("You're guess was incorrect, try again please")
    print("The computer's guess was",finalValue)
    main()

我认为问题在于,当您的输入需要被识别为整数时,它被作为字符串处理。试试这个:

def randomNumGen():
userGuess = input('The Computer has thought of a number between 1-5,     deposit    your guess now!: ')
userGuess = int(userGuess)

if finalValue == userGuess:
    print("You've guessed correctly!, Very lucky you are!")

else:
    print("You're guess was incorrect, try again please")
    print("The computer's guess was",finalValue)
    main()

可以那么问题出在哪里呢?程序总是说用户的猜测是错误的。我非常感谢您的帮助。我觉得我很具体,你们可以给我一个快速的回答。这是输出,显示了程序的明显问题:存款你的猜测:2,你的猜测不正确,计算机的猜测是2。因此,很明显,程序将无法识别用户的正确猜测。finalValue和userGuess是不同类型的。事先确保它们都是数字或字符串。根据我在这里看到的,我假设
finalValue
int
,而
userGuess
是字符串,因为这是
输入的返回类型。那么问题出在哪里呢?程序总是说用户的猜测是错误的。我非常感谢您的帮助。我觉得我很具体,你们可以给我一个快速的回答。这是输出,显示了程序的明显问题:存款你的猜测:2,你的猜测不正确,计算机的猜测是2。因此,很明显,程序将无法识别用户的正确猜测。finalValue和userGuess是不同类型的。事先确保它们都是数字或字符串。根据我在这里看到的情况,我假设
finalValue
int
,而
userGuess
是字符串,因为这是
输入的返回类型
我不确定是否正确理解了您的意思,但是问题仍然存在。我在定义userGuess的地方直接添加了“userGuess=str(finalValue)”。我也没有错误。我不确定我是否正确理解你,但是,问题仍然存在。我在定义userGuess的地方直接添加了“userGuess=str(finalValue)”。我也没有错误。