Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #%%
- def fahrenheit_to_celsius1():
- """ BAD. Does not check input before using it.
- Input from keyboard, which is always a string and must often be
- converted to an int or float.
- Converts Fahrenheit temp to Celsius.
- """
- temp_str = input("Enter a Fahrentheit temperature: ")
- temp = int(temp_str)
- newTemp = 5*(temp-32)/9
- print("The Fahrenheit temperature",temp,"is equivalent to ",end='')
- print(newTemp,"degrees Celsius")
- fahrenheit_to_celsius1()
- #%%
- """
- Test the program above by entering a temperature such as 212. Also check what
- happens if you simply press enter.
- """
- def fahrenheit_to_celsius2():
- """ IMPROVED. Does some checking of input before using it.
- Input from keyboard, which is always a string and must often be
- converted to an int or float.
- Converts Fahrenheit temp to Celsius.
- Uses 'if' to make sure an entry was made.
- """
- temp_str = input("Enter a Fahrenheit temperature: ")
- if temp_str:
- temp = int(temp_str)
- newTemp = 5*(temp-32)/9
- print("The Fahrenheit temperature",temp,"is equivalent to ",end='')
- print(newTemp,"degrees Celsius")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement