鱼C论坛

 找回密码
 立即注册
查看: 2134|回复: 8

[已解决]大家帮我看下为什么我设置了只能猜三次,但是用户输入3次以后还能继续往下输入

[复制链接]
发表于 2020-9-14 15:56:45 | 显示全部楼层 |阅读模式

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

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

x
import random

times = 3
secret = random.randint(1,10)

print('------------------我爱鱼C工作室------------------')
guess = 0
print("不妨猜一下小甲鱼现在心里想的是哪个数字:", end=" ")

while (guess != secret) and (times > 0):
    temp = input()
   
    if temp.isdigit():
        guess = int(temp)
        if guess == secret:
            print("我草,你是小甲鱼心里的蛔虫吗?!")
            print("哼,猜中了也没有奖励!")
        else:
            if guess > secret:
                print("哥,大了大了~~~")
            else:
                print("嘿,小了,小了~~~")
            if times > 1:
                print("再试一次吧:", end='')
            else:
                print("机会用光咯T_T")
    else:
        print("抱歉,您的输入有误,请输入一个整数:", end='')

    times = times - 1 # 用户每输入一次,可用机会就-1

print("游戏结束,不玩啦^_^")
最佳答案
2020-9-14 16:05:13
本帖最后由 sunrise085 于 2020-9-14 16:06 编辑
陈田甜 发表于 2020-9-14 16:01
这个代码不是的,我刚刚复制错了,复制小甲鱼的了。我的源代码是:
import random


times=times-1  这一行需要缩进,它是while循环内的一句
  1. import random

  2. times = 3
  3. secret = random .randint (1,10)
  4. print('`````````我爱鱼C工作室`````````````')
  5. guess = 0
  6. print('不妨猜一下小甲鱼现在心里想的是哪个数字:',end=" ")
  7. while(guess !=secret) and (times > 0):
  8.     temp = input()
  9.     if temp.isdigit():
  10.         guess = int(temp)
  11.         if guess == secret:
  12.             print('卧槽,你是小甲鱼心里的蛔虫吗?!')
  13.             print('哼,猜中了也没有奖励!')
  14.         else:
  15.             if guess > secret:
  16.                 print('哥,大了~大了~~~~~')
  17.             else:
  18.                 print('嘿,小了,小了~~~~')
  19.             if times > 1:
  20.                 print('再试一次吧:',end=' ')
  21.             else:
  22.                 print('机会用光咯')
  23.         
  24.     else:
  25.         print('抱歉,你的输入有误,请输入一个整数:',end='')
  26.             
  27.     times = times - 1 #这一行需要缩进
  28. print('游戏结束了,不玩了')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-9-14 15:58:46 | 显示全部楼层
你没有输入够三次吧?我试了一下,程序没问题啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-9-14 16:01:40 | 显示全部楼层
sunrise085 发表于 2020-9-14 15:58
你没有输入够三次吧?我试了一下,程序没问题啊

这个代码不是的,我刚刚复制错了,复制小甲鱼的了。我的源代码是:
import random

times = 3
secret = random .randint (1,10)
print('`````````我爱鱼C工作室`````````````')
guess = 0
print('不妨猜一下小甲鱼现在心里想的是哪个数字:',end=" ")
while(guess !=secret) and (times > 0):
    temp = input()
    if temp.isdigit():
        guess = int(temp)
        if guess == secret:
            print('卧槽,你是小甲鱼心里的蛔虫吗?!')
            print('哼,猜中了也没有奖励!')
        else:
            if guess > secret:
                print('哥,大了~大了~~~~~')
            else:
                print('嘿,小了,小了~~~~')
            if times > 1:
                print('再试一次吧:',end=' ')
            else:
                print('机会用光咯')
        
    else:
            print('抱歉,你的输入有误,请输入一个整数:',end='')
            
times = times - 1
print('游戏结束了,不玩了')
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-9-14 16:05:13 | 显示全部楼层    本楼为最佳答案   
本帖最后由 sunrise085 于 2020-9-14 16:06 编辑
陈田甜 发表于 2020-9-14 16:01
这个代码不是的,我刚刚复制错了,复制小甲鱼的了。我的源代码是:
import random


times=times-1  这一行需要缩进,它是while循环内的一句
  1. import random

  2. times = 3
  3. secret = random .randint (1,10)
  4. print('`````````我爱鱼C工作室`````````````')
  5. guess = 0
  6. print('不妨猜一下小甲鱼现在心里想的是哪个数字:',end=" ")
  7. while(guess !=secret) and (times > 0):
  8.     temp = input()
  9.     if temp.isdigit():
  10.         guess = int(temp)
  11.         if guess == secret:
  12.             print('卧槽,你是小甲鱼心里的蛔虫吗?!')
  13.             print('哼,猜中了也没有奖励!')
  14.         else:
  15.             if guess > secret:
  16.                 print('哥,大了~大了~~~~~')
  17.             else:
  18.                 print('嘿,小了,小了~~~~')
  19.             if times > 1:
  20.                 print('再试一次吧:',end=' ')
  21.             else:
  22.                 print('机会用光咯')
  23.         
  24.     else:
  25.         print('抱歉,你的输入有误,请输入一个整数:',end='')
  26.             
  27.     times = times - 1 #这一行需要缩进
  28. print('游戏结束了,不玩了')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-9-14 16:09:28 | 显示全部楼层
sunrise085 发表于 2020-9-14 16:05
times=times-1  这一行需要缩进,它是while循环内的一句

解决了谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-9-14 16:10:30 | 显示全部楼层

我很想知道你在那个帖子中所说的最后一行去掉缩进不能用是怎么一回事儿
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-9-14 16:21:31 | 显示全部楼层
sunrise085 发表于 2020-9-14 16:10
我很想知道你在那个帖子中所说的最后一行去掉缩进不能用是怎么一回事儿

我也好奇
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-9-14 16:32:10 | 显示全部楼层

一个问题发了三个帖子。。。问题解决了,人就消失了,不来结帖。。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-9-14 16:37:07 | 显示全部楼层
sunrise085 发表于 2020-9-14 16:32
一个问题发了三个帖子。。。问题解决了,人就消失了,不来结帖。。。。

哈哈  ,可能是新人,还不会弄这些东西吧!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-27 06:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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