Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 30.9.2024, more conditional statements
- print("Welcome!")
- # NEW FILE
- # our example variables, we could ask these
- # also from the user
- age = 22
- city = "Helsinki"
- # our code should give instructions to adult
- # users regarding their hometown and its health care services
- # underage users should be directed to use their own school's
- # health care services instead
- if age >= 18:
- print("Adult instructions here!")
- # this if-statement is only run, if the user is an adult (over 18)
- if city == "Rovaniemi":
- print("Health care address for adults: Test Alley 12.")
- elif city == "Helsinki":
- print("Health care address for adults: Somewhere Road 52")
- else:
- print("Underage students: contact your school's health care services!")
- # NEW FILE
- # Application description: Calculate train ticket price. The logic is this:
- # Students get always 50% discount
- # Other users pay full price + 2.5 € service fee
- # if the ticket is over 100 €, no service fee
- # ask user the needed variables, convert price to decimal format
- status = input("Student or other? (s/o)\n")
- price = input("Original ticket price? (€)\n")
- price = float(price)
- # make two different lanes for students and other users
- if status == "s":
- # students get -50% off the price
- price = price * 0.5
- elif status == "o":
- # Other users pay full price + 2.5 € service fee
- # if the ticket is over 100 €, no service fee
- # in other words: only add service fee if price is under 100
- if price < 100:
- price = price + 2.5
- # round the value to two decimals (because it's money)
- price = round(price, 2)
- print(f"Final ticket price: {price} €")
- # NEW FILE
- # example variables
- # humidity, 0-100%
- humidity = 88
- temperature = -5
- # initialize the control boolean variable: raining
- # the only purpose of this variable is to keep track of one question:
- # is it raining or not at the moment
- raining = False
- # HERE YOU WOULD HAVE ALL THE POSSIBLE LOGIC
- # THAT CAN ALTER THE CONDITION OF THE raining-variable
- # FOR EXAMPLE 300-500 lines of code
- if humidity > 80:
- raining = True
- # another check, if temperature is subzero, it's no longer rain
- if temperature < 0:
- raining = False
- # with boolean logic, you usually have an if-statement
- # like this that handles the final situation of the boolean variable
- # if raining ===> if raining == True
- # if not raining ===> if raining == False
- if raining:
- print("It rains!")
- else:
- print("It doesn't rain.")
- # NEW FILE
- # EXERCISE DESCRIPTION:
- # 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.5m/s
- # Bad weather: if it's dark outside
- # in this case, we can assume it's dark outside, if time is
- # between 20-24 or 0-7
- # initialize variables
- temperature = 13
- humidity = 65
- wind_speed = 1.5
- hour = 12
- # helper variables
- sun_down = 20
- sun_rises = 7
- # initialize our Boolean variable, that only keeps track of if the weather is good or not
- # assume in the beginning the weather is good, and try to prove it otherwise with
- # subsequent conditions
- good_weather = True
- # let's try to formulate this condition somehow
- # if you start to have a condition like this, consider a helper Boolean variable
- # to make the logic easier and more manageable
- # if temperature < 10 or humidity > 80 or wind_speed > 2.5 or (time > sun_down and ....)
- # if temperature less than 10
- if temperature < 10:
- good_weather = False
- # if humidity over 80%
- if humidity > 80:
- good_weather = False
- # wind_speed check
- if wind_speed > 2.5:
- good_weather = False
- # most difficult condition: time regarding dark
- if hour > sun_down or hour < sun_rises:
- good_weather = False
- # all checks done, we can now use the Boolean for the final time
- if good_weather:
- print("Good weather outside!")
- else:
- print("Bad weather... :(")
- # NEW FILE
- # Exercise 1. Create an application that greets the user based on given
- # hour of the current time. Convert the given hour into integer
- # format. Use the following logic while greeting the user:
- # If hour is 05 - 11 : respond "Good morning"
- # If hour is 12 - 17 : respond "Good afternoon"
- # If hour is 18 - 21 : respond "Good evening"
- # If hour is 22 - 04 : respond "Good night"
- # VERSION 1: without the advanced feature
- # PHASE 1: Ask the needed variables from user
- hour = input("Give the current hour:\n")
- hour = int(hour)
- # PHASE 2: Program logic
- # greet the user based on instructions above
- if 5 <= hour <= 11:
- print("Good morning!")
- elif 12 <= hour <= 17:
- print("Good afternoon!")
- elif 18 <= hour <= 21:
- print("Good evening!")
- else:
- print("Good night!")
- # NEW FILE
- # 1. Create an application that greets the user based on given
- # hour of the current time. Convert the given hour into integer
- # format. Use the following logic while greeting the user:
- # If hour is 05 - 11 : respond "Good morning"
- # If hour is 12 - 17 : respond "Good afternoon"
- # If hour is 18 - 21 : respond "Good evening"
- # If hour is 22 - 04 : respond "Good night"
- # Advanced feature:
- # If the user doesn't give a time (empty text), use automatically
- # the current hour of time (by using datetime-module). If the user
- # chose the automatic hour, inform user:
- # "Using current hour automatically."
- # VERSION 2: automatic hour selection before the greeting supported
- from datetime import datetime
- # PHASE 1: Ask the needed variables from user
- hour = input("Give the current hour:\n")
- # if the user gave an empty text => use automatic time
- # whether the code goes to if or else, the end result
- # will be an integer variable called hour
- if hour == "":
- # empty input => get current time from datetime
- timestamp = datetime.now()
- hour = int(timestamp.hour)
- print(f"Using current hour automatically: {hour}")
- else:
- # use the user's original time
- hour = int(hour)
- # in this spot, the hour has to be an integer
- # and the following will work correctly
- # PHASE 2: Program logic
- # greet the user based on instructions above
- if 5 <= hour <= 11:
- print("Good morning!")
- elif 12 <= hour <= 17:
- print("Good afternoon!")
- elif 18 <= hour <= 21:
- print("Good evening!")
- else:
- print("Good night!")
- # NEW FILE
- # 2. Create a simple calculator application, where user can perform the
- # following operations: +, -, * and /.
- #
- # In the beginning, ask the user two different numbers in float-format,
- # and ask finally which operation they would like to perform between the numbers.
- # After the questions, perform the selected operation between the numbers,
- # if the selections were correct.
- # If user selects division (/), check that the second number of the user
- # is not 0. If user still gives a 0 as a second number, inform the user that
- # we can't divide by zero. If user selects an operation that is not supported,
- # inform the user: "Incorrect operation".
- # Print the end result rounded to one decimal.
- # PHASE 1: Get needed variables from user and convert data types
- number1 = input("Give first number:\n")
- number1 = float(number1)
- number2 = input("Give second number:\n")
- number2 = float(number2)
- operation = input("Which operation would you like to perform? (+, -, *, /)\n")
- # initialize a variable for the final result
- result = 0
- # PHASE 2: Program logic
- if operation == "+":
- result = number1 + number2
- elif operation == "-":
- result = number1 - number2
- elif operation == "*":
- result = number1 * number2
- elif operation == "/":
- if number2 == 0:
- print("Can't divide by zero.")
- else:
- result = number1 / number2
- else:
- print("Incorrect operation")
- # PHASE 3: round values and print result
- result = round(result, 1)
- print(f"Result: {result}")
- # NEW FILE
- # 3. Create an application that asks the user the name of the
- # product and the amount of products they wish to buy. Also ask
- # the user whether they have a discount voucher (yes/no).
- # The pricing is the following:
- # - smartphone = 599 € per item
- # - computer = 899 € per item
- # - coffee machine = 129 € per item
- # Calculate the total price of the user's products.
- # If the user has a discount voucher, reduce 15% from the total price.
- # Finally, print the price rounded to two decimals, and inform them if a
- # voucher was used to reduce the price.
- # PHASE 1: Ask needed variables from user
- product = input("Which product you wish to buy? (smartphone, computer, coffee machine)\n")
- amount = input("How many items of this product would you like to buy?\n")
- amount = int(amount)
- # voucher, yes/no
- discount = input("Do you have a discount voucher? (yes/no)\n")
- # initialize result variable
- total = 0
- # PHASE 2: price logic
- if product == "smartphone":
- total = amount * 599
- elif product == "computer":
- total = amount * 899
- elif product == "coffee machine":
- total = amount * 129
- else:
- print("Unidentified product.")
- # voucher logic
- if discount == "yes":
- # 15% discount => 1 - 0.15 => 0.85
- total = total * 0.85
- # PHASE 3: round values and print
- total = round(total, 2)
- print(f"Total price: {total} €")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement