tuomasvaltanen

Untitled

Sep 30th, 2021 (edited)
504
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.30 KB | None | 0 0
  1. import math
  2.  
  3. # coding workshop 30.9.2021!
  4. print("Welcome!")
  5.  
  6. # when it comes to fractions, you can just let Python do the calculations
  7. fraction = 4 / 3
  8. result = 100 * fraction
  9.  
  10. # coding workshop 30.9.2021!
  11. print("Welcome!")
  12.  
  13. # when it comes to fractions, you can just let Python do the calculations
  14. fraction = 4 / 3
  15. result = 100 * fraction
  16.  
  17. # you can ask this from user
  18. # just remember to convert it to integer or float (int or float)
  19. radius = 15
  20.  
  21. # V    =  4/3  ·    π    ·      r^3
  22. volume = (4/3) * math.pi * math.pow(radius, 3)
  23.  
  24. # NEW FILE, tips for exercise 3.7
  25.  
  26. choice = input("Letter or parcel? (l/p):")
  27.  
  28. total_cost = 0
  29. weight = input("Give the weight (grams):")
  30. weight = int(weight)
  31.  
  32. if choice == 'l':
  33.     print("User chose letter!")
  34.     # here we should program the logic
  35.     # when sending a letter!
  36.     total_cost = total_cost + 0.5
  37.  
  38.     # here you can make if/elif/else -statements
  39.     # for the weight classes (less than 200, 200-500 and 500+)
  40.  
  41. elif choice == 'p':
  42.     print("User chose parcel!")
  43.     # and here the logic user sends a parcel!
  44.  
  45.     # here you can make if/elif/else -statements
  46.     # for the weight classes (less than 200, 200-500 and 500+)
  47. else:
  48.     print("Incorrect selection.")
  49.  
  50. print("Thank you for using our application!")
  51.  
  52. # NEW FILE
  53.  
  54. from datetime import datetime
  55.  
  56. # A BIGGER EXAMPLE, WHEN BOOLEANS ARE REALLY HELPFUL
  57. # typically the case is that you have multiple conditions
  58. # that are connected to each other or are otherwise complex
  59.  
  60. # this boolean keeps track of if the whether is good or not
  61. # we assume it's good in the beginning
  62. # the subsequent if-statements will try to prove this
  63. # weather statement WRONG! :)
  64. good_weather = True
  65.  
  66. # let's get current time (hour only)
  67. now = datetime.now()
  68. current_hour = now.strftime('%H')
  69. current_hour = int(current_hour)
  70.  
  71. # we could ask these from user as well
  72. temperature = 5
  73. humidity = 88
  74. wind_speed = 5
  75. sun_goes_down_at = 20
  76. sun_rises = 7
  77.  
  78. # OUR APPLICATION'S LOGIC:
  79. # bad weather has temperature less than 10
  80. # bad weather has humidity more than 80
  81. # bad weather has wind speed more than 2.5
  82. # bad weather, in this case, is when there is dark outside
  83. # => if before midnight, is it between 20-24 or over midnight => before 7
  84.  
  85. # the easy conditions, temperature, humidity and wind_speed
  86. # (just easy number comparisons!):
  87. if temperature < 10:
  88.     good_weather = False
  89.  
  90. if humidity > 80:
  91.     good_weather = False
  92.  
  93. if wind_speed > 2.5:
  94.     good_weather = False
  95.  
  96. # this is the tricky if-statement
  97. # is the current hour between 20 and 24
  98. if sun_goes_down_at < current_hour < 24:
  99.     good_weather = False
  100. else:
  101.     # before morning
  102.     if current_hour < sun_rises:
  103.         good_weather = False
  104.  
  105. # HERE'S SOME CODE THAT DOESN'T WORK, BUT GIVES YOU AN IDEA
  106. # WHY THIS GETS A BIT TOO DIFFICULT TO DO WITHOUT BOOLEANS
  107.  
  108. # ....so, how to check the current_hour in two separate occasions =>
  109. # if it's before midnight, or if after midnight, is it before the morning?
  110. # if temperature < 10 and humidity > 80 and wind_speed > 2.5 and
  111. #     (current_hour > sun_goes_down_at ....? HOW TO EVEN DO THIS????
  112. #     print("Bad weather!")
  113.  
  114. # but since we actually use a boolean, the last condition
  115. # is fairly simple :)
  116. if good_weather:
  117.     print("Good weather!")
  118. else:
  119.     print("Bad weather!")
  120.  
Add Comment
Please, Sign In to add comment