鱼C论坛

 找回密码
 立即注册
查看: 1107|回复: 11

[已解决]关于小游戏的改进

[复制链接]
发表于 2020-4-29 15:36:46 | 显示全部楼层 |阅读模式

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

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

x
import random
secret = random.randint(1,100)
time = 1
print("开始游戏√")
temp = input("猜猜我心里的数字:")
guess = int(temp)
if guess == secret:
    print("猜对啦!")
else:
    if guess > secret:
        print("大了")
    else:
        print("小了")
while (guess != secret) and (time<3):
    temp = input("再给你一次机会吧")
    guess = int(temp)
    if guess == secret:
        print("猜对啦!")
    else:
        if guess > secret:
            print("大了")
        else:
            print("小了")
        time = time+1
if time >= 3:
    print("你输啦")
print("游戏结束啦")
请问这样改可以么qwq
最佳答案
2020-4-29 15:52:32
只需要将while循环加一个缩进即可,要放在else里,不然第一次猜对了他还会再给你猜一次。完整代码如下:
  1. import random
  2. secret = random.randint(1,100)
  3. time = 1
  4. print("开始游戏√")
  5. temp = input("猜猜我心里的数字:")
  6. guess = int(temp)
  7. if guess == secret:
  8.     print("猜对啦!")
  9. else:
  10.     if guess > secret:
  11.         print("大了")
  12.     else:
  13.         print("小了")
  14.     while (guess != secret) and (time<3):
  15.         temp = input("再给你一次机会吧")
  16.         guess = int(temp)
  17.         if guess == secret:
  18.             print("猜对啦!")
  19.         else:
  20.             if guess > secret:
  21.                 print("大了")
  22.             else:
  23.                 print("小了")
  24.             time = time+1
  25.     if time >= 3:
  26.         print("你输啦")
  27. print("游戏结束啦")
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-9-23 03:11

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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