猿问

在 if 语句中调用函数 - Python3

因此,我尝试使用 Python3 在终端中编写登录对话,但由于某种原因,此代码:


def start():


    login_signin_Confirmation = input("Welcome! Do you wish to log-in to your account or sign up to this platform? \n")


    if login_signin_Confirmation == "login":

        

        login()

    else:

        signup()       

start()


def login():


    login_username = input("Great! start by typing in your username.")


    if login_username in users:

        print("Welcome Back, {login_username}")

login()

提示我此错误消息:


Welcome! Do you wish to log-in to your account or sign up to this platform? 

login

Traceback (most recent call last):

  File "login.py", line 22, in <module>

    start()

  File "login.py", line 19, in start

    login()

NameError: name 'login' is not defined

刚开始一些我不太确定我做错了什么,而且我找不到有同样问题的人......


呼如林
浏览 174回答 1
1回答

天涯尽头无女友

正如其他人指出的,您需要首先定义,然后调用函数:# imports first# now definitions of functionsdef start():&nbsp; &nbsp; login_signin_Confirmation = input("Welcome! Do you wish to log-in to your account or sign up to this platform? \n")&nbsp; &nbsp; if login_signin_Confirmation == "login":&nbsp; &nbsp; &nbsp; &nbsp; login()&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; signup()&nbsp; &nbsp; &nbsp; &nbsp;def signup():&nbsp; &nbsp; passdef login():&nbsp; &nbsp; login_username = input("Great! start by typing in your username.")&nbsp; &nbsp; if login_username in users:&nbsp; &nbsp; &nbsp; &nbsp; print("Welcome Back, {login_username}")# main&nbsp;def main():&nbsp; &nbsp; start()# guard multiprocess contextif __name__ == '__main__':&nbsp; &nbsp; main()
随时随地看视频慕课网APP

相关分类

Python
我要回答