#!/usr/bin/env python
from random import randint
b = randint(0, 100)
print b
a = raw_input("Try your luck, choose number (0-100): ")
if a.isdigit():
if(a == b):
print 'great!'
else:
print('wrong! that number was %s' % b)
else:
print "string isn't number"
问题是:
$ python luck.py
62个
试试你的运气,选择号码(0-100):62
错误!这个数字是62实
答案 0 :(得分:3)
您将字符串(raw_input
的结果)与数字(randint
的结果)进行比较,尝试使用
if a.isdigit():
if(int(a) == b):
答案 1 :(得分:1)
b
是一个整数。 a
是一个字符串。您需要使用int(a)
转换它。
此外,ask forgiveness rather than permission可能更加pythonic:
try:
if (int(a)==b):
print 'great!'
else:
print('wrong! that number was %s' % b)
except ValueError:
print "string isn't number"