鱼C论坛

 找回密码
 立即注册
查看: 2657|回复: 3

[萌新报道] 随机数值小游戏 这样写怎么简化啊

[复制链接]
发表于 2021-7-15 11:47:23 | 显示全部楼层 |阅读模式

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

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

x
import random
answer = random.randint(1,10)
i = 3
temp = input('请你猜猜我现在心中想的是那个数字:')
while i > 1 :
    i = i - 1
    if temp.isdigit() == 0:
        print('输入内容不合法,',end='')
        temp = input('请输入一个整数:')
    else:
        guess = int(temp)
        if guess>0 and guess <= 10:
            if guess == answer and i == 2:
                print('你是我肚子里的蛔虫吗!')
                print('不过答对了也没有奖励哦!')
                break
            elif guess == answer and i != 2:
                print('不错,答对了!')
                break
            elif guess > answer:
                print('大了大了,',end='')
                temp = input('再给你一次机会:')
            else:
                print('小了小了,',end='')
                temp = input('再给你一次机会:')
        else:
            print('输入不合法,',end='')
            temp = input('请输入0-10之间的整数:')

if  temp.isdigit() == 0:
        print('输入内容不合法,不过没机会了!')
else:
    guess = int(temp)
    if guess == answer and i == 1:
        print('机会把握住了,答对了!')
    elif guess == answer and i != 1:
        None
    else:
        print('很可惜,没机会了!')
print('game over')
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-7-15 11:56:44 | 显示全部楼层
  1. import random
  2. answer = random.randint(1,10)
  3. i = 3
  4. temp = input('请你猜猜我现在心中想的是那个数字:')
  5. while i > 1 :
  6.     i = i - 1
  7.     if temp.isdigit() == 0:
  8.         print('输入内容不合法,',end='')
  9.         temp = input('请输入一个整数:')
  10.     else:
  11.         guess = int(temp)
  12.         if guess>0 and guess <= 10:
  13.             if guess == answer and i == 2:
  14.                 print('你是我肚子里的蛔虫吗!')
  15.                 print('不过答对了也没有奖励哦!')
  16.                 break
  17.             elif guess == answer and i != 2:
  18.                 print('不错,答对了!')
  19.                 break
  20.             elif guess > answer:
  21.                 print('大了大了,',end='')
  22.                 temp = input('再给你一次机会:')
  23.             else:
  24.                 print('小了小了,',end='')
  25.                 temp = input('再给你一次机会:')
  26.         else:
  27.             print('输入不合法,',end='')
  28.             temp = input('请输入0-10之间的整数:')

  29. if  temp.isdigit() == 0:
  30.         print('输入内容不合法,不过没机会了!')
  31. else:
  32.     guess = int(temp)
  33.     if guess == answer and i == 1:
  34.         print('机会把握住了,答对了!')
  35.     elif guess == answer and i != 1:
  36.         None
  37.     else:
  38.         print('很可惜,没机会了!')
  39. print('game over')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-15 13:53:25 | 显示全部楼层
本帖最后由 jackz007 于 2021-7-15 13:55 编辑
  1. import random
  2. answer = random . randint(1,10)
  3. t = 3
  4. print('请你猜猜我现在心中想的是那个数字')
  5. print('你总共有', t , '次机会')
  6. for i in range(t):
  7.     while True:
  8.         print()
  9.         temp = input('请输入一个整数:') . strip()
  10.         if temp:
  11.             try:
  12.                 guess = int(temp)
  13.                 break
  14.             except:
  15.                 print('输入不合法!')
  16.         else:
  17.             break
  18.     if temp:
  19.         if guess == answer:
  20.             if i == 0:
  21.                 print('你是我肚子里的蛔虫吗!')
  22.                 print('不过答对了也没有奖励哦!')
  23.             else:
  24.                 print('不错,答对了!')
  25.             break
  26.         else:
  27.             if guess > answer:
  28.                 print('大了大了,' , end = '')
  29.             else:
  30.                 print('小了小了,' , end = '')                           
  31.             if i < t - 1:
  32.                 print('再给你一次机会')           
  33.     else:
  34.         break
  35. else:
  36.     print('很可惜,没机会了!')
  37. print('game over')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-7-15 14:13:52 | 显示全部楼层

刚开始学   谢谢指教
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-25 13:21

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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