|
|
发表于 2019-5-5 23:11:53
|
显示全部楼层
现有代码,如果输入非数字肯定会报错。
- import random
- from time import sleep
- def getint(msg):
- s=input(msg)
- while not s.isdigit():
- s=input('请输入数字:')
- return int(s)
- times = 3
- x = random.randint(1,10)
- print("A Small Game")
- guess=getint("Please guess which integer in my mind: ")
- if guess == x:
- print("Woa, how do you know my mind!")
- print("but i have no gift for you~")
- else :
- while (guess != x) and (times > 0):
- if guess > x:
- print("what a pity, it's too big.")
- else:
- print("what a pity, it's too small.")
- guess = getint("Please guess it again:")
- times = times - 1
- if times == 0:
- print("Sorry,you have no chance")
- if guess == x:
- print("Woa, how do you know my mind!")
- print("but i have no gift for you~")
- print("Game Over.")
复制代码 |
|