|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
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
只需要将while循环加一个缩进即可,要放在else里,不然第一次猜对了他还会再给你猜一次。完整代码如下:
- 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("游戏结束啦")
复制代码
|
|