如何做长的“如果”陈述?

时间:2019-11-16 12:09:24

标签: 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")

2 个答案:

答案 0 :(得分: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")

答案 1 :(得分: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")