执行定义的语句后," none"打印两次

时间:2016-03-22 01:23:13

标签: python function

当我运行以下代码并浏览整个request函数并将n回复到reqSecPass部分时,会打印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')

2 个答案:

答案 0 :(得分:3)

您正在打印功能的返回值。您的函数都返回None(当您不使用显式return语句时的默认返回值)。

从函数调用中删除print()语句,而不是:

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

只需使用

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

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

def goodbye():
    return 'Good Bye!'

print(goodbye())

虽然可能不值得使用函数来返回单个字符串值。

答案 1 :(得分:2)

您正在打印没有明确return语句的函数返回的值。默认情况下,此函数返回的值为None。您正在打印此值。

有时尝试使用return语句而不是始终打印,例如return 'Goodbye!'