Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Write a program that first asks the user to input numbers of shares and then
- # price information (price of each share) as whole-dollar and a fraction
- # (numerator and denominator). For example, the price string “29 7 8” stands
- # for the price 29 7/8 or 29,875. User input needs to be error checked, i.e.
- # making sure it only contains digits. Use try-except constructs for handling
- # input errors.
- # The market price should then be output with two digits after the decimal
- # point. The user should be able to repeat this as often as he/she wants.
- total_price = 0
- numerator = 1
- denominator = 1
- dollars = 1
- contin = ''
- market_price = 0
- num_shares = 0
- def shares():
- ''' Function for shares'''
- while True:
- try:
- num_shares = int(input("Enter number of shares: "))
- break
- except ValueError:
- print("Invalid number!")
- return num_shares
- def price(market_price):
- ''' Price and calculation of market price'''
- while True:
- try:
- dollars, numerator, denominator = input("Enter price in (dollars, numerator, denominator): ").split()
- int(dollars), int(numerator), int(denominator)
- 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)
- break
- except ValueError:
- print("Invalid number!")
- return market_price
- def continu():
- ''' Whether to continue or not'''
- while True:
- try:
- contin = str(input("Continue? (Y/N) "))
- if contin in 'y':
- return True
- if contin in 'n':
- return False
- except ValueError:
- print("Please only enter 'Y' or 'N'", str)
- while True:
- print(shares())
- print(price(market_price))
- if continu() == False:
- exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement