There are three main forms of conditional statements: the if statement, the if... . else statements and if... .elif... .else statements
statement
Commonly used comparison operators in conditional statements: <, <=, >, >=, ==, ! =
#Judge if you've won the lottery, winning number: 654321 number = int(input("Please enter your 6-digit lottery number: ")) if numeber == 654321: print(number,"You have won the grand prize of this issue, please come and claim your prize!!!") if number != 654321: print(number,"Unfortunately, you did not win!") # Requires the user to enter a number between 0 and 9, and prompts the user to re-enter if an illegal character is entered. number = [0,9] if ord(input("Please enter a number:") not in range(48,58): print("You entered incorrectly, please re-enter!!!")
... . else statements
# Determine if the username and password are entered correctly when logging into a website myuser = "Quiet as the morning light." mypassword = "666666" user = input("User name:") password = input("Login password:") if user == myuser and password == mypassword: print("Congratulations, login successful!") else: print("User name or password incorrect, login failed!")
... .elif... .else statements
# Determine the age class of the user year = int(input("Please enter your year of birth:")) if year >= 2010: print("You belong to the post 10, with a great future ahead of you!") elif 2010 >year >=2000: print("You belong to the 00's, Willow!") elif 2000 > year >= 1990: print("You belong in the 90s.",chopping waves and chopping rough seas (idiom); fig. hardships of the journey!) elif 1990> year >= 1980: print("You belong to the 80s and have a long way to go!") elif 1980> year >=1970: print("You belong to the 70s, the oldest of the old!")
Nesting of statements
# Determine the age class of the user year = int(input("Please enter your year of birth:")) if year >= 2010: print("You belong to the post 10, with a great future ahead of you!") elif 2010 >year >=2000: print("You belong to the 00's, Willow!") elif 2000 > year >= 1990: print("You belong in the 90s.",chopping waves and chopping rough seas (idiom); fig. hardships of the journey!) elif 1990> year >= 1980: print("You belong to the 80s and have a long way to go!") elif 1980> year >=1970: print("You belong to the 70s, the oldest of the old!")
summarize
That's all for this post, I hope it was helpful and I hope you'll check back for more from me!