python - 为什么这段代码不起作用(我是编程新手,python)

标签 python python-3.x

import random

guesses = 3
number = random.randint(1, 10)
print (number) #<- To test what number im supposed to write.

while guesses > 0: # When you have more than 0 guesses -> guess another number
    guess = input("Guess: ")

    if guess == number : # If guess is equal to number -> end game
        print ('Your guess is right!.')
        break


    if guess != number : # If not number -> -1 guess
        print ("Nope!")
        guesses -= 1

    if guesses == 0: #If all 3 guesses are made -> end game
        print("That was it! Sorry.")
        print(number,"was the right answer!")

我做错了什么? 想不通,希望大家帮忙^-^

如果您能教我如何改进我的编程,请随时写信告诉我如何去做!我愿意学习新东西顺便说一句,对不起我的英语不好 :3(编辑:当我猜对了数字时,它仍然说“不!”,我不得不猜另一个数字。)

最佳答案

这看起来像 Python3。如果是这样,请改用 guess = int(input("Guess: "))

在 Python3 中 input()返回一个字符串,您正在将该字符串与一个永远无法工作的整数进行比较。因此,将 input() 的返回值转换为一个整数,以确保您在比较苹果与苹果。

关于python - 为什么这段代码不起作用(我是编程新手,python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35368448/

相关文章:

python - 使用 for 循环创建嵌套字典

python - 如何使用opencv将轮廓作为图像获取

python - AWS CLI 中 list-objects-v2 --query 命令的 boto3 版本

python - mysqldb 查询 : not enough arguments for format string

python - pandas 数据框中重置索引可提高预测模型的 AUC

python - 异常值检测自动化

python-3.x - 在 sympy 中集成对数正态 PDF

python - 如何获取数字的最后 2 位数字(整数或小数)

Python astimezone() 意外结果

unicode - python 源代码中允许使用某些 utf8 字符,有些则不允许