在执行定义的语句之后,“无”打印两次 [英] After a defined statement is executed, "none" is printed twice

查看:147
本文介绍了在执行定义的语句之后,“无”打印两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行以下代码并遍历整个请求函数并且我回答 n reqSecPass 部分,它会打印 goodbye ,然后是两行,即 None 。如何消除这种情况?



您可以看到 goodbye 语句是独立的,我不是

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

def request():
reqPass = input('你想要哪一个密码?[Google,Twitter,Reddit,Computer]')
#still需要弄清字典操作这是一个临时系统。
if(reqPass =='google'or reqPass =='google'):
print('________________')
print('Pass:GOOGLEPASSWORDHERE')
print(' ________________')
reqSecPass = input('请求另一个密码?[y / n]')
if(reqSecPass =='y'或reqSecPass =='Y'):
print另一个())
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'或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'或reqSecPass =='Y'):
print(another())
else:
print(goodbye())
elif(reqPass = ='reddit'或reqPass =='Reddit'):
print('_________________________')
print('User:REDDITUSERNAMEHERE')
print('Pass:REDDITPASSWORDHERE')
'print'('________________')
reqSecPass = input('Request another password?[y / n]')
if(reqSecPass =='y'或reqSecPass =='Y'):
print(request())
else:
print(goodbye())
print('_____这是一个密码管理器______')
#如果用户有一个帐户
actCheck = input('您是否已经拥有账户?')
if(actC heck =='Yes'或actCheck =='yes'):
#为用户名和密码提供服务
yourUser = input('___什么是您的用户名?___')
yourPass = input ('___什么是你的密码?___')
if(yourUser =='ari'and yourPass =='rycbar1234'):
dirCheck = input('Account settings?[y,n]')
if(dirCheck =='y'或dirCheck =='Y'):
print('这个函数还没有工作!')
actSetCheck = input('更改用户名或密码'')
if(actSetCheck =='user'或actSetCheck =='User'或actSetCheck =='Username'或actSetCheck =='username'):
yourUser = input('What would you (比如你的新用户名是'')
elif(actSetCheck =='pass'或者actSetCheck =='Pass'或者actSetCheck =='password'或者actSetCheck =='Password'):
yourPass =输入('你想要什么新的用户名')
elif(dirCheck =='n'或dirCheck =='N'):
print(request())
else:
print('不正确用户名或密码')


解决方案

你的功能。你的函数都返回 None (当你不使用明确的 return 语句)时的默认返回值。 p>

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

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

只需使用

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

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

 def goodbye():
return'Good Bye!'

print(goodbye())

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


When I run the following code and go through the whole request function and I answer n to the reqSecPass part, it prints goodbye followed by two lines that say None. How do I eliminate this?

You can see that the goodbye statement is on its own and I'm not sure if this makes a difference.

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')

解决方案

You are printing the return values of your functions. Your functions all return None (the default return value when you don't use an explicit return statement).

Remove your print() statements from your function calls, so instead of:

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

just use

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

Alternatively, have your function return a string to print:

def goodbye():
    return 'Good Bye!'

print(goodbye())

although is probably not worth using a function just to return a single string value.

这篇关于在执行定义的语句之后,“无”打印两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆