小朋友们学会了吗?那么来试试怎么写一个猜数字游戏!
import random
# 没人知道我偷偷改了下,怎么感觉越写越shit?
# 定义随机数
a = int(input("请输入数值最小值:"))
b = int(input("请输入最大值:"))
random_int = random.randint(int(a), int(b))
number = random_int
# 初始化猜测变量
guess = -1
# 初始化剩余次数
num_try = int(input("难度设置:你希望有几次猜测机会?"))
print("猜数字游戏!")
while guess != number:
if num_try != 0:
print("请输入您要猜的数字(", a, "-", b, ",int)")
guess = input()
# 判断用户输入字符串类型是否为int
if guess.isdigit():
# 字符串转换为int
guess = int(guess)
if not (a <= guess <= b):
print("您的数字不合理,请重新输入!(", a, "-", b, ",int)")
continue
else:
if guess == number:
print("恭喜,你猜对了!")
print("感谢您的游玩!")
exit(0)
elif guess < number:
print("猜的数字小了...")
num_try -= 1
print("还剩", num_try, "次机会!")
continue
elif guess > number:
print("猜的数字大了...")
num_try -= 1
print("还剩", num_try, "次机会!")
continue
else:
print("您输入的是数字吗?请重新输入!")
continue
else:
print("你没有机会了!正确的数字为:", number)
print("感谢您的游玩!")
exit(0)