如何做出长时间的“如果”陈述?

2024-09-27 23:26:32 发布

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

我试图做一个长的if语句,它要求您注册或登录,但是当我进入登录部分时,出现了语法错误。有什么建议吗?你知道吗

registration = input("Do you have a registration")
if registration == "No":
    name = input("Type your name: ")
    surname = input("Type your surname: ")
    userp1 = name[0]+ surname.capitalize()
    print(userp1)
    password = input("Enter your password\n")
    userInput = input("Type your login details\n")
if userInput == userp1:
    userInput = input("Password?\n")
    if userInput== password:                 
        print("Welocome")
change = input("Do you want to change your username?")
if change == "No":
    print("You logged in as" , userp1)
else:
    userp1 = input("What would your new username be?")
    print("You logged in as",userp1)
else:
    print("Login")

Tags: nonameyouinputyouriftypepassword
2条回答

你的代码缩进不好。请注意,python对缩进非常敏感。
您也不需要指定所得到的错误。
因此,我冒昧地尝试编写一个与您的代码最大匹配的代码。
在这里:

registration = input("Do you have a registration")
if registration == "No":
   name = input("Type your name: ")
   surname = input("Type your surname: ")
   userp1 = name[0]+ surname.capitalize()
   print(userp1)
   password = input("Enter your password\n")
   userInput = input("Type your login details\n")
   if userInput == userp1:
       userInput = input("Password?\n")
       if userInput== password:                 
         print("Welocome")
   change = input("Do you want to change your username?")
   if change == "No":
      print("You logged in as" , userp1)
   else:
      userp1 = input("What would your new username be?")
      print("You logged in as",userp1)
else:
    print("Login")

您在最后一行连续编写两个else语句,这是无效语法。你可以把一个if语句放在另一个if语句中,你必须这样做。这是工作代码,但我不确定它是否是你试图使:

registration = input("Do you have a registration")
if registration == "No":
    name = input("Type your name: ")
    surname = input("Type your surname: ")
    userp1 = name[0]+ surname.capitalize()
    print(userp1)
    password = input("Enter your password\n")
    userInput = input("Type your login details\n")
    if userInput == userp1:
         userInput = input("Password?\n")
         if userInput== password:                 
           print("Welocome")
    change = input("Do you want to change your username?")
    if change == "No":
       print("You logged in as" , userp1)
    else:
       userp1 = input("What would your new username be?")
       print("You logged in as",userp1)
else:
    print("Login")

相关问题 更多 >

    热门问题