Basic Control Structures Exercises¶
Try me¶
Develop a Python script that prints “Yes, the weather is hot 🔥” if the variable ‘temperature’ is greater than 30ª Celsius, and prints “No, the weather is not hot 🧊” otherwise. Use the input function to ask the user for the temperature.
[ ]:
Modify the previous script to take into account the humidity level in percentage as well. We will consider that the temperature is hot if the temperature is higher than 30ª Celsius, but also if the temperature is higher than 24ª Celsius and the humidity is higher than 60%. Use a logical operator to combine the two conditions, and use the input function to ask the user for the humidity level.
[ ]:
Complete the following Python script so that it exists a while loop when the input provided by the user, stored in the variable “your_guess” is equal to the variable “my_name”
[ ]:
my_name = "Thomas"
your_guess = ""
Build a while loop to print the even numbers from 1 to 20 in reverse order (from 20 to 1). The sequence should be 20, 18, 16, 14, 12, 10, 8, 6, 4, 2.
[ ]:
[Optional] Modify the previous exercise to print odd numbers from 1 to 19 instead, not in reverse order, only if they cannot be divided by a previous odd number except one. For example, the number 9 is a multiple of 3, which is a previous odd number, so your script should not print 9. However, the number 11 is not a multiple of 3, so your script should print 11. The sequence should be 1, 3, 5, 7, 11, 13, 17, 19. Hint: you can use a boolean variable to check if a number can be divided by a previous odd number.
[ ]:
Analysis Questions¶
In exercise 1, what happens if the user inputs a temperature of 30ºC? Explain in your own words how would you modify the script to improve it.
How could you take into account other weather conditions, such as rain or wind? Do you think that this solution is scalable? (scalable means that it can be easily modified to add more conditions)
In exercise 3, what happens if the user inputs “Thomas” instead of “thomas”? Explain in your own words how could you use string built-in functions to make the script more robust. In this context, robust means that the script can handle different inputs and still behave as expected.
In exercise 3, How could you count the number of attempts that the user needs to guess the name? Also, how could you give hints depending on the number of attempts?
In exercise 5, how would you modify the code to reduce the number of iterations of the while loop? Explain in your own words how would you modify the script to improve it.