哎,還是有bug
temp = input("不妨猜猜我心里想的數字:")guess = int(temp)
while guess != 8 or guess != 18 or guess != 88:
temp = input("錯啦,再試試看:")
guess = int(temp)
if (1<= guess <= 100) and (guess == 8 or guess == 18 or guess == 88):
print("你妹的好亮")
else:
print("你爺的好丑")
print("game over")
按下F5之後是這樣的
Python 3.9.2 (tags/v3.9.2:1a79785, Feb 19 2021, 13:44:55) on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>>
======================== RESTART: D:/Python/shi shiba.py =======================
不妨猜猜我心里想的數字:2
錯啦,再試試看:3
你爺的好丑
錯啦,再試試看:2
你爺的好丑
錯啦,再試試看:33
你爺的好丑
錯啦,再試試看:55
你爺的好丑
錯啦,再試試看:222
你爺的好丑
錯啦,再試試看:22222
你爺的好丑
錯啦,再試試看:18
你妹的好亮
錯啦,再試試看:8
你妹的好亮
錯啦,再試試看: #这样试一下呢?
temp = input("不妨猜猜我心里想的數字:")
guess = int(temp)
while guess != 8 or guess != 18 or guess != 88:
temp = input("錯啦,再試試看:")
guess = int(temp)
if (1<= guess <= 100) and (guess == 8 or guess == 18 or guess == 88):
print("你妹的好亮")
break
else:
print("你爺的好丑")
print("game over") 当你输入的guess为8时:
guess != 8 or guess != 18 or guess != 88
(False or True) or True
(True or True)
True
所以,不管你输入8,18,88中的哪一个都会执行while循环,代码中的or应当改为and:
temp = input("不妨猜猜我心里想的數字:")
guess = int(temp)
while guess != 8 and guess != 18 and guess != 88:
temp = input("錯啦,再試試看:")
guess = int(temp)
if (1<= guess <= 100) and (guess == 8 or guess == 18 or guess == 88):
print("你妹的好亮")
else:
print("你爺的好丑")
print("game over") {:5_105:} 感谢分享! import random
def yichang():
times = 0# 次数 第一次
secrets = random.randint(1, 10)# 设置随机数字 答案
while times < 3:
try:
temp = input('猜一下小甲鱼想的数字:')# 提问
guess = int(temp)# 回答
if guess > secrets:
print('哥,大了大了!')
elif guess < secrets:
print('哥,小了小了!')
elif guess == secrets:
print('猜对了!也没有奖励。')
break
else:
print('三次机会都错了!')
break
times = times + 1
except:
print('输入无效,不是整数类型,请重新输入!!!')
continue
yichang() 感谢 学习学习 学习一波 我是这样写的,感觉还不错hhh{:10_282:}import random
times = 7
guess = 0#一定要记得赋值啊!
secret = random.randint(1,100)
print('随机了一个1到100的数,你有7次机会,来猜猜吧:',end='')#逗号千万别忘啊
while(guess != secret)and(times > 0):
times = times - 1
temp = input()
if temp.isdigit():
guess = int(temp)
if guess == secret:
print('\n' + '猜对了')
print('''
=|TAT|=======|TAT|=======|TAT|======|TAT|=
=======|-w-|=======|-w-|=======|-w-|======
=|TAT|=======|TAT|=======|TAT|======|TAT|=
=======|-w-|=======|-w-|=======|-w-|======
=|TAT|=======|TAT|=======|TAT|======|TAT|=
=======|-w-|=======|-w-|=======|-w-|======
=|TAT|=======|TAT|=======|TAT|======|TAT|=
\n还挺聪明的嘛hhh''')
else:
if guess < secret:
print('\n'*1 + '小了小了')
else:
print('\n'*1 + '大了大了')
if times > 0:
print('\n'*1 + '再试试吧:',end='')
else:
print('\n' + '机会已经用尽了,人类也不过如此嘛~')
else:
print('\n'*1 + '呃,填1到100的整数哈:)')
print('\n'*1 + '再试试吧:',end='')
没有才奇怪呢,这样才能成长 猜对了后没有跳出循环,所以还会执行
加个break就好了
temp = input("不妨猜猜我心里想的數字:")
guess = int(temp)
while guess != 8 or guess != 18 or guess != 88:
temp = input("錯啦,再試試看:")
guess = int(temp)
if (1<= guess <= 100) and (guess == 8 or guess == 18 or guess == 88):
print("你妹的好亮")
break
else:
print("你爺的好丑")
print("game over")
页:
[1]