如何加长“ if”语句?

问题描述 投票:-2回答:2

我试图在要求您进行注册或登录的地方写一个长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")
python
2个回答
0
投票

您在最后几行中连续写两个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")


0
投票

您的代码缩进得不好。请注意,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")
© www.soinside.com 2019 - 2024. All rights reserved.