Make the Program User-Friendly
00:01 The program works, and the challenge is basically finished. But still, if you look at the input requests and the output strings, it’s very basic. So in this lesson, you will adjust the strings and the input requests so it’s more user-friendly.
00:20
Let’s start by adding a new prompt
variable and giving it the value of a string, which says "Enter a temperature in degrees"
,
00:31
and then you can use this prompt
variable to format the input string. So for example, for temp_far
, you can add an f-string as the argument, and then with curly braces, add the prompt
variable in there.
00:48
And then the message in the end will say, "Enter a temperature in degrees F:"
for Fahrenheit. You can run the code and see how that looks. That looks fine, although there should be a space after your input prompt, so it looks nicer in the shell.
01:07
And then you basically do the same for temp_cel
. So you can use the prompt
variable again, put an f-string into the input()
function call as an argument,
01:17
and then it says "Enter a temperature in degrees F: "
. For the first input, you enter a number with the space in between shown. That’s nice. And then it says, "Enter a temperature in
degrees C: "
for Celsius.
01:34 It looks like we made good progress there, but it will also be nice to not only output the numbers for temperatures, but to add a little bit of context to them as well.
01:47
For this, you can again use an f-string for the print()
function calls that says {temp_far} degrees F =
and then the formatted string that you already had for displaying the Fahrenheit temperature and degrees
Celsius.
02:09
And then you can do the same for the print()
function call for the conversion of temp_cel
to temp_far
, which then says {temp_cel} degrees C =
and then the formatted string, degrees
.
02:26
Oh, and notice I have a typo up there. So it says degrees
instead of degrees F
.
02:34
All right, one last time, save the file. And when you run the module, then it says, Enter a temperature in degrees F:
When you enter 72
, then it says 72.0 degrees F = 22.22
degrees C
, and both is abbreviated with F and C.
02:54 And then the same for the Celsius degree input.
03:00
Now there is one interesting thing happening here. Since you converted the input into a float, the inputted number, 72
, is not displayed as 72
, but displayed as 72.0
because now it’s a float.
03:16
You can fix this on your own. I’m okay with that because, I mean, it’s still the same temperature, but if you want to have, like, the 72
displayed as an integer there, then you can convert it there.
03:30 But as the code is now, you can add in a number as a float value, and it will work with the f-string as well. I’m happy with the code, I’m happy with the soft challenge, and I’m curious what your solution was.
03:44 If you came up with a different piece of code, then let the Python community know in the comments below. And once you’re ready, head over to the next lesson.
John Akpan on July 7, 2024
Here is my user friendly code
def convert_cel_to_far(temp_cel):
temp_far = float(temp_cel) * ( 9 / 5 ) + 32
return round(temp_far, 2)
def convert_far_to_cel (temp_far):
temp_cel = float(temp_far - 32 ) * 5 / 9
return round(temp_cel, 2)
conversion_type = input("what is the conversion type: ? \nEnter 'A' if celcius to fahrenheit\nEnter 'B' if fahrenheit to celcius\n: ")
conversion_type = conversion_type.lower()
if conversion_type == "a":
temp_cel = (input("Enter your temperature in celcius: "))
try:
temp_far = convert_cel_to_far(temp_cel)
print(f"The temperature in celcius {temp_cel} is equivalent to {temp_far} in Farenheit")
except ValueError:
print("Please enter a valid numeric value for the temperature.")
elif conversion_type == "b":
temp_far = (input("Enter your temperature in farenheit: "))
try:
temp_cel = convert_far_to_cel(temp_far)
print(f"The temperature in celcius {temp_far} equivalent to {temp_cel} in Celcius")
except ValueError:
print("Please enter a valid numeric value for the temperature.")
soveniatkoo on Oct. 11, 2024
My solution was similar to the code shown in the video :)
def convert_cel_to_far(celsius):
fahrenheit = format(celsius * (9 / 5) + 32, ".2f")
return fahrenheit
def convert_far_to_cel(fahrenheit):
celsius = format((fahrenheit - 32) * (5 / 9), ".2f")
return celsius
prompt1 = float(input("Enter a temperature as °F: "))
user_fahrenheit = convert_far_to_cel(prompt1)
print(f"{prompt1} °F is {user_fahrenheit} °C.")
prompt2 = float(input("Enter a temperature as °C: "))
user_celsius = convert_cel_to_far(prompt2)
print(f"{prompt2} °C is {user_celsius} °F.")
Become a Member to join the conversation.
DimaG on Jan. 12, 2024
This is my solution coded in VS Code: