改进了一下数字猜谜:
开头,可选等级:
import random guess = -1 c = 0 print("数字猜谜游戏!") n = input("选择等级 A B C:") if (n == "A") or (n == "a"): guess = random.randint(1, 100) print("1到100") elif (n == "B") or (n == "b"): guess = random.randint(1, 50) print("1到50") elif (n == "C") or (n == "c"): guess = random.randint(1, 10) print("1到10")
A:1到100
B:1到50
C:1到10
中间和结尾:
当问你:“再来一局?”时
yes:再来一局
no:结束游戏
while 1: g = int(input("请输入你猜的数字:")) c += 1 if g == guess: print("恭喜,你猜对了!") print(f"你总共猜了{c}次") s = input("再来一局?:") if (s == "YES") or (s == "yes"): print("数字猜谜游戏!") n = input("选择等级 A B C:") if (s == "A") or (s == "a"): guess = random.randint(1, 100) print("1到100") elif (s == "B") or (s == "b"): guess = random.randint(1, 50) print("1到50") elif (s == "C") or (s == "c"): guess = random.randint(1, 10) print("1到10") else: break elif (s != "YES") or (s != "yes"): dy = random.randint(1, 2) if dy == 1: print("再见啦,欢迎再次玩耍") break elif dy == 2: print("拜拜啰,希望你能回来") break elif g < guess: print("猜的数字小了...") elif g > guess: print("猜的数字大了...")
(求赞🥺)