鱼C论坛

 找回密码
 立即注册
查看: 2607|回复: 5

[已解决]刚刚上到改进小游戏的萌新求助!

[复制链接]
发表于 2019-7-10 06:34:56 | 显示全部楼层 |阅读模式

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

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

x
大佬们好!
请大佬们看看我这串代码出现了什么问题?
为什么停不下来?

import random
times = 3
secret=random.randint(1,10)
print("Let's get start")
temp=input("Let's guess what i'm thinking right now:")
guess=int(temp)
while guess !=secret:
    if guess >secret:
        print("Smaller than it")
    else:
        if guess ==secret:
            print("Ahh, cool, that's good")
        else:
            print("Bigger than it")
print("that's all, you won't have more chance")
最佳答案
2019-7-10 07:35:11
  1. import random
  2. times = 3
  3. secret=random.randint(1,10)
  4. print("Let's get start")
  5. temp=input("Let's guess what i'm thinking right now:")
  6. guess=int(temp)
  7. if guess == secret:
  8.     print('Congratulations')
  9. else:
  10.     while guess !=secret and times>0:
  11.         temp=input("Trt again:")
  12.         guess=int(temp)
  13.         times -= 1
  14.         if guess >secret:
  15.             print("Smaller than it")
  16.         else:
  17.             if guess ==secret:
  18.                 print("Ahh, cool, that's good")
  19.             else:
  20.                 print("Bigger than it")
  21. print("that's all, you won't have more chance")
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-7-10 07:35:11 | 显示全部楼层    本楼为最佳答案   
  1. import random
  2. times = 3
  3. secret=random.randint(1,10)
  4. print("Let's get start")
  5. temp=input("Let's guess what i'm thinking right now:")
  6. guess=int(temp)
  7. if guess == secret:
  8.     print('Congratulations')
  9. else:
  10.     while guess !=secret and times>0:
  11.         temp=input("Trt again:")
  12.         guess=int(temp)
  13.         times -= 1
  14.         if guess >secret:
  15.             print("Smaller than it")
  16.         else:
  17.             if guess ==secret:
  18.                 print("Ahh, cool, that's good")
  19.             else:
  20.                 print("Bigger than it")
  21. print("that's all, you won't have more chance")
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-7-10 09:09:10 | 显示全部楼层
while循环中没有修改guess的值,判断永远为真,所以死循环了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-7-10 16:09:44 | 显示全部楼层
  1. import random

  2. times = 2
  3. secret=random.randint(1,10)
  4. print("Let's get start")
  5. guess = int(input("Let's guess what i'm thinking right now:"))
  6. while times:
  7.     guess = int(input("pleaese try again:"))
  8.     times -= 1
  9.     if guess == secret:
  10.         print("Ahh, cool, that's good")
  11.         break
  12.     else:
  13.         if guess > secret:
  14.             print("Bigger than it")
  15.         else:
  16.             print("Smaller than it")
  17. print("that's all, you won't have more chance")
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-7-11 01:04:18 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-7-11 01:05:35 | 显示全部楼层
BngThea 发表于 2019-7-10 09:09
while循环中没有修改guess的值,判断永远为真,所以死循环了

原来是这样,感谢大佬!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-16 10:52

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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