Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 你知道这个简单的脚本有什么问题吗_Python_Input - Fatal编程技术网

Python 你知道这个简单的脚本有什么问题吗

Python 你知道这个简单的脚本有什么问题吗,python,input,Python,Input,我所要做的就是把n变成一个随机数,如果用户得到的是正确的,它会输出“You got it!”,但不管是什么,即使我回答了n,它也会显示为我得到的是错误的 import random n = random.randint(1,2) guess = input("1 or 2?") if guess == n: print("You won!") 在python中,“10”==10是False。您必须比较相同的类型,如10==10或“10”==“10”

我所要做的就是把n变成一个随机数,如果用户得到的是正确的,它会输出“You got it!”,但不管是什么,即使我回答了n,它也会显示为我得到的是错误的

import random

n = random.randint(1,2)
guess = input("1 or 2?")
if guess == n:
  print("You won!")
在python中,
“10”==10
False
。您必须比较相同的类型,如
10==10
“10”==“10”

input()
返回一个
str
,当
random.randint()
返回一个
int
,因此
guess==n
将始终为
False

这是解决问题的简单方法:

guess = int(input("1 or 2?"))

在python中,
“10”==10
False
。您必须比较相同的类型,如
10==10
“10”==“10”

input()
返回一个
str
,当
random.randint()
返回一个
int
,因此
guess==n
将始终为
False

这是解决问题的简单方法:

guess = int(input("1 or 2?"))


这回答了你的问题吗?这回答了你的问题吗?
n = random.choice("12")