这是我的代码:
varsec =raw_input("enter secret number between 1 and 100(the system won't know):")
if int(varsec.strip()) >=100: print("Cheater caught! >:D")
if int(varsec.strip()) <=1: print("Cheater caught! >:D")
import random
varGuess = random.randint(2,99)
if 2 <= int(varsec.strip()) <= 99 : print(varGuess,
("If this number is less than your secret number, type <, if more then type >, if it IS your secret number then type =."))
VarConfirm = raw_input(":")
if (VarConfirm.strip()) is "<": from random import randint
print(randint(varGuess + 1,99))
if (VarConfirm.strip()) is ">": from random import randint
print(randint(2,varGuess - 1))
if (VarConfirm.strip()) is "=": print("YES, the program has successfully guessed your number ^^")
如果我输入&#34;&lt;&#34;对于VarConfirm
,它会打印两个数字(1个数字介于varGuess +1
和99
之间,另一个是2
和varGuess -1
,包括在内。
我希望它只打印varGuess + 1
和99
之间的数字。
此外,当我输入"<"
或"="
时,会出现NameError: 'randint' is not defined.
答案 0 :(得分:4)
您没有缩进print
语句。无论if
测试如何,它们都会始终执行。
Python通过缩进形成块;你需要将所有只能在if
测试为真时执行的行放在同一个缩进处,并且缩进需要大于if
行本身:
if (VarConfirm.strip()) is "<":
from random import randint
print(randint(varGuess + 1,99))
if (VarConfirm.strip()) is ">":
from random import randint
print(randint(2,varGuess - 1))
请注意,我将import
语句放在新行上。您可以将'block'放在与if
语句相同的行上,但是该块仅限于一个简单的语句;你做不止一件事。
您也不应该使用is
来测试字符串相等性;这些测试通常应该失败,因为raw_input()
函数不需要返回用于测试的与内存完全相同的对象。无论如何,CPython都是一个你不能依赖的内部实现细节。您想测试他们的值是否相等:
if (VarConfirm.strip()) == "<":
from random import randint
print(randint(varGuess + 1,99))
if (VarConfirm.strip()) == ">":
from random import randint
print(randint(2,varGuess - 1))
但是,您的导入会将新的全局变量添加到您的文件中;只需将留在脚本的顶部:
from random import randint
# other code
if (VarConfirm.strip()) == "<":
print(randint(varGuess + 1,99))
if (VarConfirm.strip()) == ">":
print(randint(2,varGuess - 1))
或只使用random.randint()
;您之前已导入random
模块:
if (VarConfirm.strip()) == "<":
print(random.randint(varGuess + 1,99))
if (VarConfirm.strip()) == ">":
print(random.randint(2,varGuess - 1))
你不需要使用这么多括号;在Python 2中print
是一个语句,而不是一个函数(就像return
):
if VarConfirm.strip() == "<":
print random.randint(varGuess + 1,99)
if VarConfirm.strip() == ">":
print random.randint(2,varGuess - 1)
因为您的测试是互斥的,所以您可以使用if ... elif ...
,Python只需要继续测试,直到测试的一个为止:
if VarConfirm.strip() == "<":
print random.randint(varGuess + 1,99)
elif VarConfirm.strip() == ">":
print random.randint(2,varGuess - 1)
elif VarConfirm.strip() == "=":
print "YES, the program has successfully guessed your number ^^"
您可以避免经常调用.strip()
;只需调用一次并存储结果:
# ask for the input and strip immediately before storing the result:
VarConfirm = raw_input(":").strip()
if VarConfirm == "<":
print random.randint(varGuess + 1,99)
elif VarConfirm == ">":
print random.randint(2,varGuess - 1)
elif VarConfirm == "=":
print "YES, the program has successfully guessed your number ^^"
答案 1 :(得分:0)
我不知道你是否在这里复制代码搞砸了你的缩进,但你的缩进表明if语句中“:”之后的部分算作唯一条件部分,并且那么print语句只是普通的python行,完全独立于if语句。
此外,没有必要在if语句的中间导入类似的东西。只需在程序开头创建import语句,然后再使用它。所以你的代码应该是
from random import randint
# [.... previous steps ....]
if (VarConfirm.strip()) == "<":
print randint(varGuess + 1,99)
if (VarConfirm.strip()) == ">":
print randint(2,varGuess-1)
if (VarConfirm.strip()) == "=":
print "Yes, bla bla"