萌新求助
import randomcounts = 10
answer = random.randint(1,100)
while counts > 0:
temp = input("你来猜猜我现在心里想的是什么数字")
guess = int(temp)
if guess == answer:
print("不错嘛,有点意思")
print("哎!这可是无奖竞猜没奖励的啦")
break
else:
if guess < answer:
print("太小啦,你不会是细狗吧")
else:
print("太大了," + "\n" + "我被什么蒙蔽了双眼")
if counts > 0:
counts = counts -1
extracounts = "你还有" + str(counts) + "次机会"
print(extracounts)
else:
print("你行不行啊,细狗")
print("就只是个作业而已")
各位大佬,我这个代码不知道为什么机会用完的情况下"你行不行啊,细狗"这句话弹不出来,
还有就是怎么让还剩0次机会这句话不出现在末尾嘞
求助求助{:5_104:}
因为在 counts <= 0 的时候循环的条件就不成立了,就会直接跳出循环
解决方法:把倒数第三行 else: 改为 if counts <= 0: 本帖最后由 jackz007 于 2022-10-31 11:27 编辑
import random
counts = 10
answer = random.randint(1,100)
while counts > 0:
temp = input("你来猜猜我现在心里想的是什么数字")
guess = int(temp)
if guess == answer:
print("不错嘛,有点意思")
print("哎!这可是无奖竞猜没奖励的啦")
break
else:
if guess < answer:
print("太小啦,你不会是细狗吧")
else:
print("太大了," + "\n" + "我被什么蒙蔽了双眼")
if counts > 1: # 只是在还有剩余机会的时候才提示剩余次数
extracounts = "你还有" + str(counts) + "次机会"
counts = counts -1 # 这一句应该无条件执行
else:
print("你行不行啊,细狗")
print("就只是个作业而已")
页:
[1]