社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
私信  •  关注

Abdelbaki Souid

Abdelbaki Souid 最近创建的主题
Abdelbaki Souid 最近回复了
3 年前
回复了 Abdelbaki Souid 创建的主题 » Python if and else语句不是预期的输出

我注意到一些错误。 首先,您不需要if-trument==3或2,因为您使用的是while循环(while循环基于最初的条件)。 第二,最好将“break”积分为一个无限循环。 而循环从0开始,在2结束(取3个值)。

我为你重写了代码。

def check_guess(guess, answer):
global score
global name
still_guessing = True
attempt = 0
while still_guessing and attempt < 2:
    if guess == answer:
        print('Correct wow, i expected worse from someone named %s' % name)
        if attempt == 0:
            score = score + 3
        elif attempt == 1:
            score = score + 2
        elif attempt == 2:
            score = score + 1
        still_guessing = False
    elif attempt <= 1:
        print('HAAAAAAAAAAAAAAAAAAAAAAAAAAAA IMAGINE NOT GETTING THAT RIGHT!!!')
        print('L bozo + ratio')
        guess = input('Try again ')
        attempt = attempt + 1
    break

print('the correct answer was %s' % answer)
print('There is always next time!!')
still_guessing = False

要测试代码,请添加 print("pass OK")

score = 0
print('Welcome to the animal quiz')
print()
print('In this game you will have to guess the animal')
name = input('What is your name? ')
print('Cool name, I feel like i heard of it before hmmm %s..' % name)
print()
print('Enough stalling onto the game!!!')
guess1 = input('Which bear lives in the north pole ')
check_guess(guess1.strip().lower(), 'polar bear')
print("pass OK")

别忘了投票:)(斋月卡里姆!!!)