社区所有版块导航
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
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

Python if and else语句不是预期的输出

Kbeast • 3 年前 • 1108 次点击  

我想用python 3.9做一个动物测试。在其中一个问题上,犯了三次错并不能像应该的那样开始一个新问题。相反,它只是一片空白。在第一次和第二次尝试时,一切都很顺利。感谢您的任何帮助。这是我的代码:

def check_guess(guess, answer):
    global score
    global name
    still_guessing = True
    attempt = 0
    while still_guessing and attempt < 3:
        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
    if attempt == 3:
        print('the correct answer was %s' % answer)
        print('There is always next time!!')
        still_guessing = False
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')
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/130615
 
1108 次点击  
文章 [ 5 ]  |  最新文章 3 年前
Abdelbaki Souid
Reply   •   1 楼
Abdelbaki Souid    3 年前

我注意到一些错误。 首先,您不需要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")

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

user1702660
Reply   •   2 楼
user1702660    3 年前

在你的while循环中 “尝试”永远不会变为3,因此代码无法跳转到下一部分 if attmpt == 3 所以在while循环中 elif 情况应该是 elif attempt <= 2: 然后 attempt = attempt + 1 可以达到3

Raunak Raj
Reply   •   3 楼
Raunak Raj    3 年前

使用下面的代码它对我有用。

def check_guess(guess, answer):
    global score
    global name
    still_guessing = True
    attempt = 0
    while still_guessing and attempt < 3:
        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 <= 2:
            print('HAAAAAAAAAAAAAAAAAAAAAAAAAAAA IMAGINE NOT GETTING THAT RIGHT!!!')
            print('L bozo + ratio')
            guess = input('Try again ')
            attempt = attempt + 1
    if attempt == 3:
        print('the correct answer was %s' % answer)
        print('There is always next time!!')
        still_guessing = False
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')
Ralph Macêdo
Reply   •   4 楼
Ralph Macêdo    3 年前

我认为 elif 贝娄应该评估<=2,而不是1:

        elif attempt <= 2:

但最后一条“再试一次”的信息仍然打印出来。您可以在“重试”消息之前设置一个尝试检查条件来解决这个问题。如果条件求值为True,则中断循环:

        elif attempt <= 2:
            print('HAAAAAAAAAAAAAAAAAAAAAAAAAAAA IMAGINE NOT GETTING THAT RIGHT!!!')
            print('L bozo + ratio')
            attempt = attempt + 1
            if attempt > 2:
                break            
            guess = input('Try again ')

在这种情况下,请记住调整您的状态,因为不再需要尝试检查。

如果可以的话,我在代码中做了一些重构,因此您可以查看其他方法来实现相同的目标。

def check_guess(guess, answer, attempts):
    global score
    global name
    while True:
        if guess == answer:
            print('Correct wow, i expected worse from someone named %s' % name)
            score = [0, 1, 2, 3]
            final_score = score[attempts]
            print(f'Score: {final_score}')
            break

        attempts -= 1

        if attempts == 0:
            print('the correct answer was %s' % answer)
            print('There is always next time!!')
            break
        
        print('HAAAAAAAAAAAAAAAAAAAAAAAAAAAA IMAGINE NOT GETTING THAT RIGHT!!!')
        print('L bozo + ratio')
        guess = input('Try again ')

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', attempts=3)
rushBblyat
Reply   •   5 楼
rushBblyat    3 年前

你必须把 if attempt == 3 在你的while循环中,因为while循环无限地运行,直到猜测是正确的,所以当 attempt 的值是3,它将什么也不做,因为它仍然在一个循环中,并且没有if语句告诉它值为3后该做什么。

编辑:还将循环条件更改为 still guessing and attempt <= 3

attempt = 0
def check_guess(guess, answer):
    global score
    global name
    global attempt
    still_guessing = True

    while still_guessing and attempt <= 3:
        print(f"attempt {attempt}")
        if attempt == 2:
            print('the correct answer was %s' % answer)
            print('There is always next time!!')
            still_guessing = False
    
        else:
            print('HAAAAAAAAAAAAAAAAAAAAAAAAAAAA IMAGINE NOT GETTING THAT RIGHT!!!')
            print('L bozo + ratio')
            guess = input('Try again ')
            attempt = attempt + 1

        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