jeudi 29 avril 2021

How to avoid using to much if else?

This is the concept of what I'm trying to acheive with the code. I want the user to be able to leave the login section whenever they press '0', the codes below works, but is there anyway that I can shorten these codes, cause I felt like there's too much if else being use.

def signup():
     print("Press '0' to return mainpage")
     firstname = input("Please enter your first name : ")
     if firstname == "0":
          print("Mainpage returned")
          mainpage()
     else:
          lastname  = input("Please enter your last name  : ")
          if lastname == "0":
               print("Mainpage returned")
               mainpage()
          else:
               username = firstname+lastname
               print("Welcome", username)

def mainpage():
     option = input("Press '1' to signup: ")
     if option == '1':
          print("Sign up page accessed")
          signup()
mainpage()

Aucun commentaire:

Enregistrer un commentaire