caihong199216 发表于 2021-7-20 16:08:43

哎,還是有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
你妹的好亮
錯啦,再試試看:

迎风听雨 发表于 2021-7-20 16:23:48

#这样试一下呢?
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")

临时号 发表于 2021-7-21 14:57:29

当你输入的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")

majian890324 发表于 2021-7-21 17:21:01

{:5_105:}

hornwong 发表于 2021-7-21 19:00:45

感谢分享!

wcq15759797758 发表于 2021-7-21 19:09:08

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()

fxj2002 发表于 2021-7-21 19:30:11

感谢

超级玛尼哄 发表于 2021-7-21 22:34:11

学习学习

burntlime 发表于 2021-7-22 13:56:20

学习一波

1001101 发表于 2021-7-22 22:53:38

我是这样写的,感觉还不错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='')   
   
   


江湖散人 发表于 2021-7-23 01:15:31

没有才奇怪呢,这样才能成长

叼辣条闯世界 发表于 2021-7-24 14:58:25

猜对了后没有跳出循环,所以还会执行
加个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]
查看完整版本: 哎,還是有bug