Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.17 KB | None | 0 0
  1. #%%
  2. def fahrenheit_to_celsius1():
  3.     """ BAD. Does not check input before using it.
  4.    Input from keyboard, which is always a string and must often be
  5.    converted to an int or float.
  6.    Converts Fahrenheit temp to Celsius.
  7.    """
  8.    
  9.     temp_str = input("Enter a Fahrentheit temperature: ")
  10.     temp = int(temp_str)
  11.     newTemp = 5*(temp-32)/9
  12.     print("The Fahrenheit temperature",temp,"is equivalent to ",end='')
  13.     print(newTemp,"degrees Celsius")
  14.    
  15. fahrenheit_to_celsius1()
  16. #%%
  17. """
  18. Test the program above by entering a temperature such as 212. Also check what
  19. happens if you simply press enter.
  20. """
  21.  
  22. def fahrenheit_to_celsius2():
  23.     """ IMPROVED. Does some checking of input before using it.
  24.    Input from keyboard, which is always a string and must often be
  25.    converted to an int or float.
  26.    Converts Fahrenheit temp to Celsius.
  27.    Uses 'if' to make sure an entry was made.
  28.    """
  29.    
  30.     temp_str = input("Enter a Fahrenheit temperature: ")
  31.     if temp_str:
  32.         temp = int(temp_str)
  33.         newTemp = 5*(temp-32)/9
  34.         print("The Fahrenheit temperature",temp,"is equivalent to ",end='')
  35.         print(newTemp,"degrees Celsius")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement