鱼C论坛

 找回密码
 立即注册
查看: 2751|回复: 1

[作品展示] 猜数字(防沉迷改进版)

[复制链接]
发表于 2022-8-31 14:13:32 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
猜数字游戏,快来和我一起创作吧
代码复制到IDLE中即可运行!
  1. import random
  2. try:
  3.     h=open("temp.txt")
  4.     temp=h.read()
  5.     h.close()
  6. except FileNotFoundError:
  7.     h=open("temp.txt","w")
  8.     h.close
  9.     h=open("temp.txt","r+")
  10.     temp=h.read()
  11.     h.close()
  12. if temp=='1':
  13.     a=input("你已经试过了,输入1即可再玩一次!")
  14.     try:
  15.         if a=="1":
  16.             f = open("temp.txt","r+")
  17.             f.write("2")
  18.             f.close()
  19.             secret = random.randint(1,10)
  20.             temp = input("不妨猜一下我现在心里想的是哪个数字(1到10):")
  21.             guess = int(temp)
  22.             times = 1
  23.             while (guess != secret) and (times < 3):
  24.                 if guess > secret:
  25.                     print("哥,大了大了~~~")
  26.                 else:
  27.                     print("嘿,小了小了~~~")
  28.                 temp = input("请再试试吧:")
  29.                 guess = int(temp)
  30.                 times = times + 1
  31.             if times < 3:
  32.                 print("哎呀,你是我心里的蛔虫吗?!")
  33.                 print("哼,猜中了也没有奖励噢~")
  34.                 input()
  35.             else:
  36.                 print("唔,给三次机会都猜错,不跟你玩了!")
  37.                 input()
  38.         else:
  39.             print("输入错误")
  40.             input()
  41.     except:
  42.         print("输入错误")
  43. elif temp=="":
  44.     print("为响应国家防沉迷措施,最多玩两次哦!")
  45.     f = open("temp.txt","r+")
  46.     f.write("1")
  47.     f.close()
  48.     secret = random.randint(1,10)
  49.     temp = input("不妨猜一下我现在心里想的是哪个数字(1到10):")
  50.     guess = int(temp)
  51.     times = 1
  52.     while (guess != secret) and (times < 3):
  53.         if guess > secret:
  54.             print("哥,大了大了~~~")
  55.         else:
  56.             print("嘿,小了小了~~~")
  57.         temp = input("请再试试吧:")
  58.         guess = int(temp)
  59.         times = times + 1
  60.     if times < 3:
  61.         print("哎呀,你是我心里的蛔虫吗?!")
  62.         print("哼,猜中了也没有奖励噢~")
  63.         input()
  64.     else:
  65.         print("唔,给三次机会都猜错,不跟你玩了!")
  66.         input()
  67. elif temp=='2':
  68.     print("超过游戏限制!")
  69.     input()
复制代码

如果有bug,可以帮忙改进吗?谢谢!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-8-31 18:04:09 | 显示全部楼层
感觉有点复杂了,看看原版的猜数游戏:
  1. import random

  2. print('在 1 ~ 100 之间猜一个数')

  3. result = random.randint(1, 100)  # 随机一个数

  4. while True:
  5.     x = input('请猜一个数:')
  6.     x = int(x)
  7.     if x == result:
  8.         print('猜对了!游戏结束。')
  9.         break
  10.     elif x < result:
  11.         print('猜小了,请继续;', end='')
  12.     elif x > result:
  13.         print('猜大了,请继续;', end='')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-27 01:53

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表