在elif语句中调用函数

时间:2018-08-28 22:05:52

标签: python function variables if-statement

我知道这不是编写代码的最佳方法,但是我想解决现有问题-不必改变整个事情。我知道有些地方代码会停止,因为我还没有为其创建任何其他内容。此时,我要修复的问题是,一旦调用initial_username函数,并输入N作为Confirm变量的输入,它仍然会打印if语句下的内容,而不是在elif语句内调用该函数。

def wrong_data():
    print("I'm sorry, I didn't get that.")
    fix_name()

def fix_name():
    print("Let's try that again.")
    fn_error= input('Please re-enter your first name: ')
    ln_error= input('Please re-enter your last name: ')
    second_confirm= input('Is your full name ' + str(fn_error) + ' ' + str(ln_error) + '? Please enter Y or N: ')
    if second_confirm== 'Y' or 'y':
        print('Thank you, ' + str(fn_error) + ' ' + str(ln_error) + '.')

def initial_username():
user_fn= input('Please enter your first name: ')
user_ln= input('Please enter your last name: ')
confirm= input('Is your full name ' + str(user_fn) + ' ' + str(user_ln) + '? Please enter Y or N: ')
if confirm == 'Y'or 'y':
    print('Thank you, ' + str(user_fn) + ' ' + str(user_ln) + '.')
elif confirm == 'N' or 'n':
    fix_name()
else:
    wrong_data()

initial_username()

1 个答案:

答案 0 :(得分:2)

无论confirm == 'Y'是否为True,您的陈述将始终为True,因为单个'y'将始终为True。

if confirm in ('Y', 'y'):
    print('Thank you, ' + str(user_fn) + ' ' + str(user_ln) + '.')
elif confirm in ('N', 'n'):
    fix_name()
else:
    wrong_data()