我无法让用户输入除整数之外的任何内容,因为这样做会导致程序崩溃而不会打印我想要的文本。
这是终端中的程序。
答案 0 :(得分:1)
你需要对python 2.x使用raw_input()
guess = raw_input()
还有代码
elif: #Need help here!
语法无效 - 它需要条件子句。但是看看你的代码,你可以将“elif:”删除到“break”,因为它只是复制了它下面的内容。
答案 1 :(得分:0)
第6行&你应该使用
print 'No its '+str(ans)
答案 2 :(得分:0)
from random import randint
import sys
# Python version compatibility shim
if sys.hexversion < 0x3000000:
# Python 2.x
inp = raw_input
rng = xrange
else:
# Python 3.x
inp = input
rng = range
TRIES = 10
def main():
print("Welcome to the Math Challenge!")
correct = 0
for m in rng(1, TRIES + 1):
n = randint(1, 16)
answer = m * n
guess = inp("What is {} x {}? ".format(m, n))
try:
guess = int(guess)
if guess == answer:
print("That's right")
correct += 1
else:
print("Sorry, it's actually {}".format(answer))
except ValueError:
# guess is not an int value
guess = guess.strip().lower()
if guess == "skip":
print("Skipping...")
continue
elif guess == "stop":
print("Stopping...")
break
print("You got {} correct out of {}!".format(correct, TRIES))
if __name__ == "__main__":
main()