|
发表于 2019-8-11 12:31:07
|
显示全部楼层
可以使用字符串的 isdigit() 方法有效地防止报错
- print("-------------------------欢迎来到数字炸弹游戏(╯‵□′)╯•••*~●-----------------------------------------")
- import random
- secret = random.randint(1,100)
- flag = 1
- temp = input("猜猜熊宝宝心里在想什么数吧(ง•_•)ง(只能输入正整数哦!):\n")
- # 用字符串的 isdigit() 方法判断用户输入的是不是有效的整数
- while not temp.isdigit():
- temp = input("输入错误啦(ง•_•)ง只能输入正整数哦!\n")
- guess = int(temp)
- while(flag):
- if guess > secret:
- print("大啦大啦,哈哈O(∩_∩)O")
- temp = input("再来试试吧:\n")
- guess = int(temp)
- elif guess < secret:
- print("小啦小啦,呵呵( ̄▽ ̄)")
- temp = input("再来试试吧:\n")
- guess = int(temp)
- else:
- print("炸弹来啦(╯‵□′)╯•••*~●")
- print("boom!爆炸")
- flag = 0
- print("游戏结束,不玩啦ヾ(•ω•`)o")
复制代码 |
|