This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Prompt the user until a valid numeric input is entered."""
while True:
try:
return float(input(prompt))
except ValueError:
print("Invalid input. Please enter a numeric value.")
def convert_cel_to_far(temp_cel):
"""Return the Celsius temperature temp_cel converted to Fahrenheit."""
temp_far = temp_cel * (9 / 5) + 32
return temp_far
return temp_cel * (9 / 5) + 32
def convert_far_to_cel(temp_far):
"""Return the Fahrenheit temperature temp_far converted to Celsius."""
temp_cel = (temp_far - 32) * (5 / 9)
return temp_cel
return (temp_far - 32) * (5 / 9)
# Prompt the user to input a Fahrenheit temperature.
temp_far = input("Enter a temperature in degrees F: ")
temp_far = get_numeric_input("Enter a temperature in degrees F: ")
# Convert the temperature to Celsius.
# Note that `temp_far` must be converted to a `float`
# since `input()` returns a string.
temp_cel = convert_far_to_cel(float(temp_far))
temp_cel = convert_far_to_cel(temp_far)
# Display the converted temperature
print(f"{temp_far} degrees F = {temp_cel:.2f} degrees C")
# You could also use `round()` instead of the formatting mini-language:
# print(f"{temp_far} degrees F = {round(temp_cel, 2)} degrees C"")
# Prompt the user to input a Celsius temperature.
temp_cel = input("\nEnter a temperature in degrees C: ")
temp_cel = get_numeric_input("\nEnter a temperature in degrees C: ")
# Convert the temperature to Fahrenheit.
temp_far = convert_cel_to_far(float(temp_cel))
temp_far = convert_cel_to_far(temp_cel)
# Display the converted temperature
print(f"{temp_cel} degrees C = {temp_far:.2f} degrees F")
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
feat: add input validation to temperature conversion exercise #90
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Are you sure you want to change the base?
Uh oh!
There was an error while loading. Please reload this page.
feat: add input validation to temperature conversion exercise #90
Filter by extension
Viewed files
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
There are no files selected for viewing