簡體   English   中英

如何制作長“if”語句?

[英]How to make long “if” statement?

我正在嘗試做一個長的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")

您在最后幾行連續編寫了兩個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")

您的代碼沒有很好的縮進。 請注意 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")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM