我似乎无法在Python 2.7中运行a == x [3]。我输入了正确的数字并且它没有说“恭喜”#39;我告诉它说声明是否正确 另外,如何重复十次而不必复制和粘贴代码十次。非常感谢提前!
import time
import random
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46,47,48,49,50]
print "WELCOME TO THE BEST GUESSING GAME IN THE WORLD! GUESS THE RIGHT NUMBER AND YOU WIN A BRAND-NEW CAR"
random.shuffle(x)
time.sleep(2)
print "YOU GET 10 TRIES"
time.sleep(1)
print "GOOD LUCK!"
time.sleep(2)
a = raw_input('Enter number 1-50 ')
time.sleep(2)
if a == x[3]:
print 'Congrats'
else:
print 'NO'
a = raw_input('Enter number 1-50 ')
time.sleep(2)
if a == x[3]:
print 'Congrats'
else:
print 'NO'
a = raw_input('Enter number 1-50 ')
time.sleep(2)
if a == x[3]:
print 'Congrats'
else:
print 'NO'
a = raw_input('Enter number 1-50 ')
time.sleep(2)
if a == x[3]:
print 'Congrats'
else:
print 'NO'
a = raw_input('Enter number 1-50 ')
time.sleep(2)
if a == x[3]:
print 'Congrats'
else:
print 'NO'
a = raw_input('Enter number 1-50 ')
time.sleep(2)
if a == x[3]:
print 'Congrats'
else:
print 'NO'
a = raw_input('Enter number 1-50 ')
time.sleep(2)
if a == x[3]:
print 'Congrats'
else:
print 'NO'
a = raw_input('Enter number 1-50 ')
time.sleep(2)
if a == x[3]:
print 'Congrats'
else:
print 'NO'
a = raw_input('Enter number 1-50 ')
time.sleep(2)
if a == x[3]:
print 'Congrats'
else:
print 'NO'
a = raw_input('Enter number 1-50 ')
time.sleep(2)
if a == x[3]:
print 'Congrats'
else:
print 'NO'
a = raw_input('Enter number 1-50 ')
time.sleep(2)
if a == x[3]:
print 'Congrats'
else:
print 'NO'
time.sleep(1)
print 'YOU FAILED!!'
print x[3]
答案 0 :(得分:2)
raw_input
将返回一个字符串。你需要先把它转换为int!使用int(raw_input('...'))
。
此外,int()
在您向其提供无法转换为字符串的内容时会崩溃,因此您可能希望使用try/catch
语句将其包围,以处理不正确的输入以防止意外崩溃。
您可以使用for
循环来避免复制粘贴代码。如果您不熟悉计算机科学并且正在学习python,请查看非常受欢迎的Learn Python the Hard Way。
答案 1 :(得分:1)
您的raw_input
是str
。将其设为int
,例如:
a = int(raw_input('Enter number 1-50 '))