Ive tried messing around with the code a bit but i cant see the problem, idk if I'm just being blind but there's nothing there.
我试着把代码弄乱了一点,但我看不出有什么问题,如果我只是瞎了眼,但那里什么都没有。
Originally I had an if then else
but it wasn't working so I tried making a nested if
to specify that if it isn't the number, it says wrong.
最初我有一个If Then Else,但它不起作用,所以我尝试创建一个嵌套的If,以指定如果它不是数字,它就会显示错误。
import random
userstart = input("Please type START to play.")
if userstart == "START" or userstart == "start":
print("Game starting, \n")
num1 = random.randint(0, 10)
print(num1)
usernum = input("Guess that number!")
print(usernum)
if num1 == usernum:
print("Congrats you win!")
if num1 != usernum:
print("WRONG\n")
print("The number was: ", num1)
更多回答
Be sure you convert the input to an integer before comparing. The number 5
and the string "5"
are not equal to each other.
确保在比较之前将输入转换为整数。数字5和字符串“5”不相等。
By the way, this will be a lot more obvious if you get in the habit of printing the repr()
of your values (by default, print(x)
acts like print(str(x))
, not print(repr(x))
).
顺便说一句,如果您养成了打印值的epr()的习惯,这一点就会更加明显(默认情况下,print(X)类似于print(str(X)),而不是print(epr(X)。
As pointed out, but more specifically, try using usernum = int(input('Guess that number! '))
. Note the use of int()
to convert the input (of type str
) into an int
type for matching to the (pseudo-)randomly generated integer.
如上所述,但更具体地说,尝试使用usernum=int(INPUT(‘猜猜那个数字!’))。注意,使用int()将输入(类型为str)转换为int类型,以匹配(伪)随机生成的整数。
优秀答案推荐
In python, input
is a built-in function that returns the user input as a string
. randint
on the other hand is a function imported from the standard random
module which returns an int
.
在Python中,Input是一个内置函数,它以字符串形式返回用户输入。而Randint则是从标准随机模块导入的函数,它返回一个int。
So the comparison done in the first if statement,if num1 == usernum:
results in False
, as a str
literal is never equal to an int
literal.
因此,在第一个if语句中执行的比较,if num1==usernum:结果为FALSE,因为str文本永远不等于int文本。
To solve this issue, you may typecast the input into an int
using the int()
constructor:
要解决此问题,可以使用int()构造函数将输入类型转换为int:
usernum = int(input("Guess the number!"))
更多回答
我是一名优秀的程序员,十分优秀!