今天花了一点时间用python写了个猜数小游戏,10分钟就能搞定!
废话不多说,直接上代码
由于代码太过于简单,所以没有什么注释。
import random
print("欢迎来到猜数小游戏!")
print("作者:Debug618\n")
cnt = 0
total_cnt = 0
while 1:
tot = 0
if cnt == 0:
tot = int(input("1.开始游戏\n2.结束游戏\n"))
while tot != 1 and tot != 2:
tot = int(input("1.开始游戏\n2.结束游戏\n"))
else:
tot = int(input("1.继续游戏\n2.结束游戏\n"))
while tot != 1 and tot != 2:
tot = int(input("1.继续游戏\n2.结束游戏\n"))
if int(tot) == 2:
break
l, r = input("请输入数的范围(两个数,用逗号隔开):").split(",")
num = random.randint(int(l), int(r)) # 求出随机数
your = int(input("请输入你猜的数:"))
cai = 0
if your == num:
print("你太厉害了吧!");
cai += 1
while 1:
# 判断是否猜对,并给出提示
if your < num:
print("数太小了!")
your = int(input("请输入你猜的数:"))
cai += 1
elif your > num:
print("数太大了!")
your = int(input("请输入你猜的数:"))
cai += 1
if your == num:
print("恭喜你答对了!")
print("你猜了" + str(cai) + "次!\n")
total_cnt += cai
break
cnt += 1
print("---统计---\n你玩了" + str(cnt) + "局\n你一共猜了" + str(total_cnt) + "次\n")
今天的代码就是这些了,整体代码量还是很少的,也非常简单,新手也能写
那么我们下次再见!