Question
python question: Task 1 Prompt the user for a temperature value, and assign it to a variable (e.g., celsius) Calculate the Fahrenheit value of this
python question:
Task 1
Prompt the user for a temperature value, and assign it to a variable (e.g., celsius)
Calculate the Fahrenheit value of this variable, and store it in a new variable (e.g., fahrenheit)
Print both numbers to the console, e.g.,
Temperature in Celsius: 20.5 Temperature in Fahrenheit: 68.9
Hint: The formula for converting Celsius to Fahrenheit is 1.8 * c + 32 = f
Try different ways of printing the above output (e.g., f-strings, concatenation, etc.).
Think about which way you think looks best, in terms of code neatness!
Task 2
Improve this program! The user should only be able to enter sensible numeric values for temperatures in Celsius (e.g., 4.7, -10, 143.5).
Start by adding the following code to the top of your program. Ill go through it in more detail later in the course. Note: if copying from a PDF viewer, the indentation levels may not get copied correctly. If this happens, be sure to manually add the indentation levels (4 spaces per level).
def isfloat(num): try:
float(num)
return True except ValueError:
return False
The above code will enable you to check if a given string is a float value, by writing, e.g., isfloat(celsius).
Now, modify your code from Task 3, using the isfloat(...) function to make sure that the user has entered a value between, say, -200 and 200.
If the value is valid, the program continues and converts the value to Fahrenheit as before.
Otherwise, the program should tell the user that their value was invalid, and should prompt
them for a new one.
If youre doing this with an if-else block, it will only check once whether the value is valid.
For bonus points, try to have the program repeat the check until the user enters a valid value.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started