Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Adobe Connect -lecture, 27.9.2021
- print("Conditional statements today!")
- # ask the user's age and convert it into an integer
- age = input("How old are you?\n")
- age = int(age)
- # check if age is less or at least 20
- if age < 20:
- print("You're less than 20 years old.")
- elif age < 30:
- print("You're less than 30 years old.")
- else:
- print("You're at least 30 years old.")
- print("This is always printed, no matter what the age is!")
- # NEW FILE
- # ask the user's age and convert it into an integer
- age = input("How old are you?\n")
- age = int(age)
- # less than
- if age < 30:
- print("You're less than 30 years old.")
- # more or exactly 30
- if age >= 30:
- print("You're at least 30 years old.")
- # exactly 30
- if age == 30:
- print("You are exactly 30 years old.")
- # anything else but 30
- if age != 30:
- print("You're not exactly 30 years old.")
- # NEW FILE
- # comparing variables
- # ask number from user
- number1 = input("Give a number:\n")
- number1 = int(number1)
- number2 = 234
- # compare it to the other variable
- if number1 < number2:
- print("Number 1 is smaller.")
- else:
- print("Number 2 is smaller.")
- # NEW FILE
- # checking for a word in if-statement
- text = input("Give some text:\n")
- if text == "banana":
- print("You found the magic word!")
- else:
- print("You didn't find it... :(")
- # NEW FILE
- # a typical selection logic in many exercises
- choice = input("Are you a student, or an adult? (s/a)\n")
- if choice == 's':
- print("Calculate the price for a student.")
- elif choice == 'a':
- print("Calculate the price for an adult.")
- else:
- print("Incorrect selection.")
- # NEW FILE
- # you can ask this from user too!
- number = 12
- # a classic, is the number even or odd?
- if number % 2 == 0:
- print("Even.")
- else:
- print("Odd.")
- # NEW FILE
- number = input("Give number:\n")
- number = int(number)
- # we can have multiple comparisons in a single statement
- if 0 <= number <= 30:
- print("The number is between 0 and 30.")
- if number < 0 or number > 30:
- print("The number is outside 0 and 30.")
- # NEW FILE
- city = "Rovaniemi"
- age = 20
- student = True
- # comparing to multiple variables
- # ALL OF THESE HAVE TO MATCH
- if age >= 18 and city == 'Rovaniemi' and student == True:
- print("An adult student from Rovaniemi.")
- else:
- print("Not an adult student from Rovaniemi.")
- # NEW FILE
- # nested if-statement,
- # an if-statement inside another if-statement
- number = 17
- if number % 2 == 0:
- print("Even")
- if number > 10:
- print("That's a big even number!")
- else:
- print("Odd")
- # NEW FILE
- # the variable scope example from slides
- temperature = 17
- is_hot = False
- if temperature > 20:
- is_hot = True
- print(temperature)
- else:
- print(temperature)
- print(is_hot)
- # NEW FILE
- # booleans are helpful
- temperature = -12
- humidity = 86
- # we assume at first, it's not raining
- raining = False
- # very humid => raining
- if humidity > 80:
- raining = True
- # if it's cold, it's not raining, it's a hailstorm
- if temperature < 0:
- raining = False
- # THIS PART COULD HAVE HUNDREDS LINES OF CODE
- if raining:
- print("It rains!")
- else:
- print("Doesn't rain.")
- # NEW FILE
- # creating a boolean from input, example 1
- # THIS CAN BE DONE IN MANY WAYS
- choice = input("Is it raining? (y/n)\n")
- raining = False
- if choice == 'y':
- raining = True
- if raining:
- print("RAINS!")
- else:
- print("No rain.")
Advertisement
Add Comment
Please, Sign In to add comment