在一个定义的语句被执行之后,“none”被打印两次

2024-09-29 19:21:54 发布

您现在位置:Python中文网/ 问答频道 /正文

当我运行下面的代码并遍历整个request函数时,我回答nreqSecPass部分,它会打印goodbye,后跟两行None。如何消除此问题?你知道吗

您可以看到goodbye语句是独立的,我不确定这是否有区别。你知道吗

def goodbye():
    print('Good Bye!')

def request():
    reqPass = input('Which password would you like?[Google, Twitter, Reddit, Computer]')
    #still need to figure out dictionary manipulation so this is a temporary sytem.
    if(reqPass == 'google' or reqPass == 'google'):
        print('________________')
        print('Pass: GOOGLEPASSWORDHERE')
        print('________________')
        reqSecPass = input('Request another password?[y/n]')
        if(reqSecPass == 'y' or reqSecPass == 'Y'):
            print(another())
        else:
            print(goodbye())
    elif(reqPass == 'twitter' or reqPass == 'Twitter'):
        print('_________________')
        print('User: TWITTERUSERNAMEHERE')
        print('Pass: TWITTERPASSWORDHERE')
        print('________________')
        reqSecPass = input('Request another password?[y/n]')
        if(reqSecPass == 'y' or reqSecPass == 'Y'):
            print(another())
        else:
            print(goodbye())
    elif(reqPass == 'computer' or reqPass == 'Computer'):
        print('________________')
        print('Pass: COMPUTERPASSWORDHERE')
        print('________________')
        reqSecPass = input('Request another password?[y/n]')
        if(reqSecPass == 'y' or reqSecPass == 'Y'):
            print(another())
        else:
            print(goodbye())
    elif(reqPass == 'reddit' or reqPass == 'Reddit'):
        print('_________________________')
        print('User: REDDITUSERNAMEHERE')
        print('Pass: REDDITPASSWORDHERE')       
        print('________________')
        reqSecPass = input('Request another password?[y/n]')
        if(reqSecPass == 'y' or reqSecPass == 'Y'):
            print(request())
        else:
            print(goodbye())
print('_____This is a password keeper_____')
#checking if the user has an account
actCheck = input('Do you already have an account?')
if(actCheck == 'Yes' or actCheck == 'yes'):
    #asking for user's name and password
    yourUser = input('___What is your Username?___')
    yourPass = input('___What is your Password?___')
    if(yourUser == 'ari' and yourPass == 'rycbar1234'):
        dirCheck = input('Account settings?[y,n]')
        if(dirCheck == 'y' or dirCheck == 'Y'):
            print('this function is not working yet!')
            actSetCheck = input('Change username or password?')
            if(actSetCheck == 'user' or actSetCheck == 'User' or actSetCheck == 'Username' or actSetCheck == 'username'):
                yourUser = input('What would you like your new username to be?')
            elif(actSetCheck == 'pass' or actSetCheck == 'Pass' or actSetCheck == 'password' or actSetCheck == 'Password'):
                yourPass = input('What would you like your new username to be?')
        elif(dirCheck == 'n' or dirCheck == 'N'):
            print(request())
    else:
        print('Incorrect Username or password')

Tags: orinputifisanotherpasspasswordelse
2条回答

您正在打印函数的返回值。所有函数都返回None(不使用显式return语句时的默认返回值)。你知道吗

从函数调用中删除print()语句,以便:

print(another())
# ...
print(goodbye())
# ...
print(request())

只是使用

another()
# ...
goodbye()
# ...
request()

或者,让函数返回要打印的字符串:

def goodbye():
    return 'Good Bye!'

print(goodbye())

尽管仅仅返回单个字符串值可能不值得使用函数。你知道吗

您正在打印没有显式return语句的函数返回的值。默认情况下,从这样一个函数返回的值是None。您正在打印此值。你知道吗

有时尝试使用return语句而不是总是打印,例如return 'Goodbye!'。你知道吗

相关问题 更多 >

    热门问题