Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.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 2.7 Python的基本问题_Python 2.7 - Fatal编程技术网

Python 2.7 Python的基本问题

Python 2.7 Python的基本问题,python-2.7,Python 2.7,我正在使用python 2.7.9 我在下面写了这段代码,当我在Shell中运行时,随机数显示出来,我输入它,然后就什么也没发生了。我没有收到错误消息,但我不明白为什么它不打印“Good Guess” 你的问题是,它在猜测中以字符串的形式读取。所以它将字符串与整数进行比较。这总是False。改为 guess = int(raw_input("Guess my Number: ")) 使之坚固 try: guess = int(raw_input("Guess my Number: ")

我正在使用python 2.7.9

我在下面写了这段代码,当我在Shell中运行时,随机数显示出来,我输入它,然后就什么也没发生了。我没有收到错误消息,但我不明白为什么它不打印“Good Guess”


你的问题是,它在猜测中以字符串的形式读取。所以它将字符串与整数进行比较。这总是
False
。改为

guess = int(raw_input("Guess my Number: "))
使之坚固

try:
    guess = int(raw_input("Guess my Number: "))
except ValueError:
    print "that wasn't an integer."
    continue

然后,您可以根据自己的需要对此进行循环。

您从不调用任何东西,而且
原始输入总是给出一个字符串。(我更正了缩进。问题是,要告诉stackoverflow它是代码,您需要缩进所有可能重复的内容(包括
导入随机
定义…
try:
    guess = int(raw_input("Guess my Number: "))
except ValueError:
    print "that wasn't an integer."
    continue