Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # lecture 3, 29.9.2023, Conditional statements
- print("Welcome!")
- # NEW FILE
- age = 19
- # conditional statement -> is age under 20?
- # if it is -> run the print-code
- # if not, skip all the code inside the if-statement
- if age < 20:
- print("You are less than 20 years old.")
- print("This is also printed, if age is less than 20")
- print("There's no limit on how much code is inside a conditional statement.")
- print("This is printed, no matter the case (not inside the if-statement)")
- # NEW FILE
- # ask user for the age
- age = input("How old are you?\n")
- age = int(age)
- # if age is less than 20 -> print text
- # else (in other case) -> print something else
- # else is basically the opposite of if age < 20 at this point
- # => 20 years old or more
- if age < 20:
- print("You're under 20 years old")
- else:
- print("You're something else (basically 20 years old or more)")
- # NEW FILE
- # ask user for the age
- age = input("How old are you?\n")
- age = int(age)
- # if age is less than 20 -> print text
- # else (in other case) -> print something else
- # else is basically the opposite of if age < 20 at this point
- # => 20 years old or more
- if age < 20:
- print("You're under 20 years old")
- elif age < 30:
- print("You're under 30 years old")
- elif age < 40:
- print("You're under 40 years old")
- else:
- print("Your age is something else.")
- print("Thank you for using the application!")
- # NEW FILE
- # ask user for the age and month
- age = input("How old are you?\n")
- age = int(age)
- month = input("Provide month (number):\n")
- month = int(month)
- # if age is less than 20 -> print text
- # else (in other case) -> print something else
- # else is basically the opposite of if age < 20 at this point
- # => 20 years old or more
- if age < 20:
- print("You're under 20 years old")
- elif age < 30:
- print("You're under 30 years old")
- elif age < 40:
- print("You're under 40 years old")
- else:
- print("Your age is something else.")
- # different if-statement, always checked separately
- # of the if-statement above
- if month == 7:
- print("Sorry, we're on vacation.")
- print("Shop open again in August.")
- print("Thank you for using the application!")
- # NEW FILE
- # ask user the price in float format
- price = input("Give the price:\n")
- price = float(price)
- age = input("How old are you?\n")
- age = int(age)
- # modify price based on age
- if age < 18:
- # if user under 18 -> discount to price 10%
- price = price * 0.9
- else:
- # if not under 18 -> no discount, add postage
- price = price + 4.95
- print(f"Total sum: {price} €")
- # NEW FILE
- # ask user for the age
- age = input("How old are you?\n")
- age = int(age)
- if age < 30:
- print("Less than 30 years old.")
- # if possible, use this format most of the time
- # less than equal to
- # no need to do something age < 30 or age == 30: etc.
- if age <= 30:
- print("Less than or equal to 30 years old.")
- if age > 30:
- print("More than 30 years old.")
- # this is rarely need, usually we want to compare
- # to ranges instead of actual number values
- # sometimes needed, for example if product_id == 0
- # => a missing product in a webshop
- if age == 30:
- print("You're exactly 30 years old")
- # this is the opposite of == .... not EXACTLY 30 years old
- if age != 30:
- print("You're not exactly 30 years old")
- # NEW FILE
- # ask one number from user
- # remember to always convert your input into int/float
- # comparing to text value creates strange bugs
- number1 = input("Give a number:\n")
- number1 = int(number1)
- number2 = 234
- # if one of these numbers is still in text-format
- # (no int/float used), this can be very buggy
- if number1 > number2:
- print("The first number is bigger!")
- else:
- print("The second number is bigger!")
- # NEW FILE
- # ask user for a sales code
- sales_code = input("Give your potential sales code:\n")
- # this is our current sales code accepted for a discount
- # this season (a web shop for example)
- current_code = "WINTER23"
- # let's see if the user's sales code is valid or not
- # usually text is compared only with == or !=
- # banana < firetruck wouldn't make any sense...
- if sales_code == current_code:
- print("Discount to price, - 20%!")
- else:
- print("Normal price.")
- # NEW FILE
- # ask for user input
- drink = input("What would like to drink?\n")
- # if-elif -> react to different user responses
- # else -> drink not founc
- if drink == "milk":
- print("Price of milk: 1€")
- elif drink == "coffee":
- print("One pack of coffee: 6.5€")
- elif drink == "water":
- print("Free, use the tap water.")
- else:
- print("Product not found.")
- # NEW FILE
- # let's make a simple two-path application
- # ask user if they are student or not
- choice = input("Are you a student or not? (s/o)\n")
- # if student
- if choice == 's':
- print("This code only considers students")
- print("e.g. calculate some kind of special price etc.")
- elif choice == 'o':
- # if other user
- print("Calculate price for other customers")
- else:
- print("Incorrect selection. Please restart the program.")
- # NEW FILE
- # ask user for a number
- number = input("Give a number:\n")
- number = int(number)
- # when we divide by two, if remainder is exactly == 0
- if number % 2 == 0:
- print("Even number!")
- else:
- print("Odd number!")
- # NEW FILE
- number = input("Give a number:\n")
- number = int(number)
- # is number either 0, or 0-30
- if number >= 0 and number < 30:
- print("Number is between 0 - 30")
- # is number less than 0, or more than 30
- if number < 0 or number >= 30:
- print("Number is less than 0, or 30 or over.")
- # NEW FILE
- number = input("Give a number:\n")
- number = int(number)
- # a preferred shorthand for 0 - 30
- # compare to mathematics
- if 0 <= number < 30:
- print("Number is between 0 - 30")
- # is number less than 0, or more than 30
- if number < 0 or number >= 30:
- print("Number is less than 0, or 30 or over.")
- # NEW FILE
- year = int(input("Give a year (2000-2012):\n"))
- # easy to compare a bunch of ranged values
- # always double-check all the comparisons
- # and try your code with test values
- # ESPECIALLY on the boundary values
- # example: 2000, 2004, 2008, 2012
- if 2000 <= year < 2004:
- print("2000-2004")
- elif 2004 <= year < 2008:
- print("2004-2008")
- elif 2008 <= year <= 2012:
- print("2008-2012")
- else:
- print("Incorrect year")
- # NEW FILE
- # some variables
- # you could ask these from user as well
- age = 20
- city = "Helsinki"
- student = True
- # also possible to combine many variables if needed
- # typically "less is more" is the way to go
- if age >= 18 and city == "Rovaniemi" and student == True:
- print("An adult student from Rovaniemi")
- else:
- print("Something else.")
- # NEW FILE
- age = 20
- city = "Helsinki"
- # first filter out users that are 18 or more
- if age >= 18:
- # this part of code only considers adult users
- print("Adult!")
- if city == "Rovaniemi":
- print("Adult health care in address Test Street 12.")
- if city == "Helsinki":
- print("Adult health care in address Experiment Plaza 35.")
- print("Easiest to arrive by tram or bus.")
- else:
- # under 18 users get this response instead
- # all of the if-statements above, will be ignored
- print("For persons under 18 years old, consult your school health care.")
- # NEW FILE
- # ask user for price
- price = input("Price in EUR?\n")
- price = float(price)
- # give starting value to discount to prevent variable scope errors
- discount = 0
- # if price is over 300€ -> discount of 50€
- if price > 300:
- discount = 50
- else:
- # if price 300€ or less -> +10€ postage added
- price = price + 10
- # calculate final price including everything
- total = price - discount
- print(f"Total sum: {total}€")
- # NEW FILE
- # variables, could be asked from user
- # humidity 0-100%
- humidity = 92
- temperature = -5
- raining = False
- if humidity > 80:
- raining = True
- # subzero temperatures, not actually rain anymore
- if temperature < 0:
- raining = False
- # THIS PART COULD POTENTIALLY HAVE HUNDREDS OF LINES OF CODE
- # SWITCHING THE RAINING BOOLEAN IN DIFFERENT POSITIONS
- # final decision
- if raining:
- print("It's raining outside!")
- else:
- print("It's not raining at the moment.")
- # NEW FILE
- # ask the dtails from user
- status = input("Student or adult? (s/a)\n")
- price = input("Original ticket price?:\n")
- price = float(price)
- # s == student
- # a == adult
- if status == 's':
- # student get 50% discount
- price = price * 0.5
- elif status == 'a':
- # only add service fee
- # if price is less than 100€
- if price < 100:
- price = price + 2.5
- print(f"Final price: {price} €")
- # NEW FILE
- # EXERCISE DESCRIPTION:
- # Let's make an application, that determines based on variables
- # if it's a GOOD or BAD weather outside
- # The logic of the weather is this:
- # Bad weather: if temperature is less than +10C
- # Bad weather: if humidity is over 80%
- # Bad weather: if wind speed is over 2.5 m/s
- # Bad weather: if it's dark outside
- # in this case we assume it's dark outside, if time is 20-24 or 0-7
- # intialize variables
- temperature = 15
- humidity = 32
- wind_speed = 1.4
- time = 18
- # the boolean that keeps track of bad/good weather
- good_weather = True
- # helper variables
- sun_down = 20
- sun_rises = 7
- # this condition gets very complex and unmaintainable
- # if you start to have a condition like this, consider a helper Boolean variable
- # if temperature < 10 or humidity > 80 or wind_speed > 2.5 or (time > sun_down ....)
- # if temperature less than 10
- if temperature < 10:
- good_weather = False
- # humidity over 80%
- if humidity > 80:
- good_weather = False
- # is it too windy
- if wind_speed > 2.5:
- good_weather = False
- # is it dark at the moment
- if time > sun_down or time < sun_rises:
- good_weather = False
- # the final result - let's see what is the
- # result of the boolean in the end
- if good_weather:
- print("Good weather outside!")
- else:
- print("Bad weather .... :(")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement