Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- # coding workshop 30.9.2021!
- print("Welcome!")
- # when it comes to fractions, you can just let Python do the calculations
- fraction = 4 / 3
- result = 100 * fraction
- # coding workshop 30.9.2021!
- print("Welcome!")
- # when it comes to fractions, you can just let Python do the calculations
- fraction = 4 / 3
- result = 100 * fraction
- # you can ask this from user
- # just remember to convert it to integer or float (int or float)
- radius = 15
- # V = 4/3 · π · r^3
- volume = (4/3) * math.pi * math.pow(radius, 3)
- # NEW FILE, tips for exercise 3.7
- choice = input("Letter or parcel? (l/p):")
- total_cost = 0
- weight = input("Give the weight (grams):")
- weight = int(weight)
- if choice == 'l':
- print("User chose letter!")
- # here we should program the logic
- # when sending a letter!
- total_cost = total_cost + 0.5
- # here you can make if/elif/else -statements
- # for the weight classes (less than 200, 200-500 and 500+)
- elif choice == 'p':
- print("User chose parcel!")
- # and here the logic user sends a parcel!
- # here you can make if/elif/else -statements
- # for the weight classes (less than 200, 200-500 and 500+)
- else:
- print("Incorrect selection.")
- print("Thank you for using our application!")
- # NEW FILE
- from datetime import datetime
- # A BIGGER EXAMPLE, WHEN BOOLEANS ARE REALLY HELPFUL
- # typically the case is that you have multiple conditions
- # that are connected to each other or are otherwise complex
- # this boolean keeps track of if the whether is good or not
- # we assume it's good in the beginning
- # the subsequent if-statements will try to prove this
- # weather statement WRONG! :)
- good_weather = True
- # let's get current time (hour only)
- now = datetime.now()
- current_hour = now.strftime('%H')
- current_hour = int(current_hour)
- # we could ask these from user as well
- temperature = 5
- humidity = 88
- wind_speed = 5
- sun_goes_down_at = 20
- sun_rises = 7
- # OUR APPLICATION'S LOGIC:
- # bad weather has temperature less than 10
- # bad weather has humidity more than 80
- # bad weather has wind speed more than 2.5
- # bad weather, in this case, is when there is dark outside
- # => if before midnight, is it between 20-24 or over midnight => before 7
- # the easy conditions, temperature, humidity and wind_speed
- # (just easy number comparisons!):
- if temperature < 10:
- good_weather = False
- if humidity > 80:
- good_weather = False
- if wind_speed > 2.5:
- good_weather = False
- # this is the tricky if-statement
- # is the current hour between 20 and 24
- if sun_goes_down_at < current_hour < 24:
- good_weather = False
- else:
- # before morning
- if current_hour < sun_rises:
- good_weather = False
- # HERE'S SOME CODE THAT DOESN'T WORK, BUT GIVES YOU AN IDEA
- # WHY THIS GETS A BIT TOO DIFFICULT TO DO WITHOUT BOOLEANS
- # ....so, how to check the current_hour in two separate occasions =>
- # if it's before midnight, or if after midnight, is it before the morning?
- # if temperature < 10 and humidity > 80 and wind_speed > 2.5 and
- # (current_hour > sun_goes_down_at ....? HOW TO EVEN DO THIS????
- # print("Bad weather!")
- # but since we actually use a boolean, the last condition
- # is fairly simple :)
- if good_weather:
- print("Good weather!")
- else:
- print("Bad weather!")
Add Comment
Please, Sign In to add comment