P5改进我们的小游戏——数字炸弹小游戏
看完视频也写了一个小游戏,但又一点不友好,就是只能输入数字,不然会报错请各位鱼油多多指教
print("-------------------------欢迎来到数字炸弹游戏(╯‵□′)╯•••*~●-----------------------------------------")
import random
secret = random.randint(1,100)
flag = 1
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")
可以使用字符串的 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")
页:
[1]