|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- import random
- secret = random.randint(1,100)
- guess = 0
- count = 3
- while count >0:
- temp = input("Guess how old am I: ")
- guess = int(temp)
- if guess <= 0 or guess > 100:
- print("Please input a number between 1 and 100.")
- elif guess == secret:
- print("Holy cow! Smarty pants!")
- break
- else:
- if guess > secret:
- print("It's too big.")
- else:
- print("It's too small.")
- print("Try again!")
- count = count - 1
- 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”呢?
本帖最后由 liuzhengyuan 于 2018-7-18 11:41 编辑
可以用一个if来判断是否要输出try again。
如
- import random
- secret = random.randint(1,100)
- guess = 0
- count = 3
- while count > 0:
- temp = input("Guess how old am I: ")
- guess = int(temp)
- if guess <= 0 or guess > 100:
- print("Please input a number between 1 and 100.")
- elif guess == secret:
- print("Holy cow! Smarty pants!")
- break
- else:
- if guess > secret:
- print("It's too big.")
- else:
- print("It's too small.")
- if count > 1:
- print("Try again!")
- count = count - 1
- 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.
|
|