Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.92 KB | None | 0 0
  1. # Write a program that first asks the user to input numbers of shares and then
  2. # price information (price of each share) as whole-dollar and a fraction
  3. # (numerator and denominator). For example, the price string “29 7 8” stands
  4. # for the price 29 7/8 or 29,875. User input needs to be error checked, i.e.
  5. # making sure it only contains digits. Use try-except constructs for handling
  6. # input errors.
  7. # The market price should then be output with two digits after the decimal
  8. # point. The user should be able to repeat this as often as he/she wants.
  9.  
  10.  
  11.  
  12. total_price = 0
  13. numerator = 1
  14. denominator = 1
  15. dollars = 1
  16. contin = ''
  17. market_price = 0
  18. num_shares = 0
  19.  
  20. def shares():
  21.     ''' Function for shares'''
  22.     while True:
  23.         try:
  24.             num_shares = int(input("Enter number of shares: "))
  25.             break
  26.         except ValueError:
  27.             print("Invalid number!")
  28.     return num_shares
  29.  
  30. def price(market_price):
  31.     ''' Price and calculation of market price'''
  32.     while True:
  33.         try:
  34.             dollars, numerator, denominator = input("Enter price in (dollars, numerator, denominator): ").split()
  35.             int(dollars), int(numerator), int(denominator)
  36.             market_price = dollars + (numerator/denominator) # Current problem <- Gives a unsupported operand errror    (Need to return price that is input. e.g. 89 1 2 = 89.5)
  37.             break
  38.         except ValueError:
  39.             print("Invalid number!")
  40.     return market_price
  41.  
  42. def continu():
  43.     ''' Whether to continue or not'''
  44.     while True:
  45.         try:
  46.             contin = str(input("Continue? (Y/N) "))
  47.             if contin in 'y':
  48.                 return True
  49.             if contin in 'n':
  50.                 return False
  51.         except ValueError:
  52.             print("Please only enter 'Y' or 'N'", str)
  53. while True:
  54.     print(shares())
  55.     print(price(market_price))
  56.     if continu() ==  False:
  57.         exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement