鱼C论坛

 找回密码
 立即注册
查看: 706|回复: 4

[已解决]python004猜数字求助

[复制链接]
发表于 2018-7-12 21:56:12 | 显示全部楼层 |阅读模式

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

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

x
  1. import random
  2. secret = random.randint(1,100)
  3. guess = 0
  4. count = 3

  5. while count >0:
  6.     temp = input("Guess how old am I: ")
  7.     guess = int(temp)
  8.     if guess <= 0 or guess > 100:
  9.         print("Please input a number between 1 and 100.")
  10.     elif guess == secret:
  11.         print("Holy cow! Smarty pants!")
  12.         break
  13.     else:
  14.         if guess > secret:
  15.             print("It's too big.")
  16.         else:
  17.             print("It's too small.")
  18.         print("Try again!")
  19.     count = count - 1
  20. print("Game over.")
复制代码


这个代码出来之后是这样的:
Guess how old am I: 23
It's too small.
Try again!
Guess how old am I: 36
It's too small.
Try again!
Guess how old am I: 88
It's too big.
Try again!
Game over.

求助各位大神,怎样让使用最后一次猜测机会错误时,不显示“Try again”呢?
最佳答案
2018-7-14 20:50:00
本帖最后由 liuzhengyuan 于 2018-7-18 11:41 编辑

可以用一个if来判断是否要输出try again。

  1. import random
  2. secret = random.randint(1,100)
  3. guess = 0
  4. count = 3

  5. while count > 0:
  6.     temp = input("Guess how old am I: ")
  7.     guess = int(temp)
  8.     if guess <= 0 or guess > 100:
  9.         print("Please input a number between 1 and 100.")
  10.     elif guess == secret:
  11.         print("Holy cow! Smarty pants!")
  12.         break
  13.     else:
  14.         if guess > secret:
  15.             print("It's too big.")
  16.         else:
  17.             print("It's too small.")
  18.         if count > 1:
  19.             print("Try again!")
  20.     count = count - 1
  21. print("Game over.")
复制代码



运行效果:
Guess how old am I: 4
It's too small.
Try again!
Guess how old am I: 5
It's too small.
Try again!
Guess how old am I: 88
It's too big.
Game over.
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-7-12 22:26:58 From FishC Mobile | 显示全部楼层
在打印这一句前面加一个判断
if count > 1:
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 3 反对 0

使用道具 举报

发表于 2018-7-14 20:50:00 | 显示全部楼层    本楼为最佳答案   
本帖最后由 liuzhengyuan 于 2018-7-18 11:41 编辑

可以用一个if来判断是否要输出try again。

  1. import random
  2. secret = random.randint(1,100)
  3. guess = 0
  4. count = 3

  5. while count > 0:
  6.     temp = input("Guess how old am I: ")
  7.     guess = int(temp)
  8.     if guess <= 0 or guess > 100:
  9.         print("Please input a number between 1 and 100.")
  10.     elif guess == secret:
  11.         print("Holy cow! Smarty pants!")
  12.         break
  13.     else:
  14.         if guess > secret:
  15.             print("It's too big.")
  16.         else:
  17.             print("It's too small.")
  18.         if count > 1:
  19.             print("Try again!")
  20.     count = count - 1
  21. print("Game over.")
复制代码



运行效果:
Guess how old am I: 4
It's too small.
Try again!
Guess how old am I: 5
It's too small.
Try again!
Guess how old am I: 88
It's too big.
Game over.
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-7-17 22:15:12 | 显示全部楼层
BngThea 发表于 2018-7-12 22:26
在打印这一句前面加一个判断
if count > 1:

謝謝!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-7-17 22:16:26 | 显示全部楼层
liuzhengyuan 发表于 2018-7-14 20:50
可以用一个if来判断是否要输出try again。

謝謝你 這樣一目瞭然
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-25 21:19

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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