2015.5.25第一天下载Python IDLE,写个猜数字的小游戏来熟悉这门语言:
times=6
letters=[100]
for i in range(1,times):
a = input("input the number you guess:")
try:
b = int(a)
if isinstance(b,int):
if i <5:
if int(a) in letters:
print("%s is the right answer, you win!" % a)
break
else:
sen1 = "%s is not the right answer," % a
if int(a) < letters[0]:
sen2 = " and it is smaller,please try again."
else:
sen2 = " and it is greater, please try again."
sen = "%s%s" % (sen1,sen2)
print("%s" % sen)
continue
else:
print("You lose!")
else:
print("You should input an integer number")
except Exception as err:
print("You should input an integer number.")
input("Input any key to quit:")
只有4次机会,都猜错了就算输,包括对数据输入的类型控制,就这样: