Beispiel #1
0
def main():
	while True: 
		try:
			meal_base = sys.argv[1]
		except IndexError:
			try:
				meal_base = float(raw_input("\nIndexError: It seem sys.argv[1] is out of index range. \nInput the base cost of your meal. \nUse only numbers. No symbols(i.e 10.50, 234.33, 5.00): "))
				break
			except ValueError:
				meal_base = float(raw_input("\nValueError: That's not a valid int or float \nInput the base cost of your meal. \nUse only numbers. No symbols(i.e 10.50, 234.33, 5.00): "))
				break
			except ZeroDivisionError:
				meal_base = float(raw_input("\nZeroDivisionError: You can't pick zero! \nInput the base cost of your meal. \nUse only numbers. No symbols(i.e 10.50, 234.33, 5.00): "))
				break
			except NameError:
				meal_base = float(raw_input("\nNameError: That's not a valid int or float. \nInput the base cost of your meal. \nUse only numbers. No symbols(i.e 10.50, 234.33, 5.00): "))
				break
			finally:
				print "\nThe base cost of your meal is", meal_base

	while True: 
		try:
			tax_rate = sys.argv[2]
		except IndexError:
			try:
				tax_rate = float(raw_input("\nIndexError: It seem sys.argv[2] is out of range. \nInput the tax rate as a decimal. \n(i.e, 10% would be .1, 15% would be .15 or 5% would be .05): "))
				break
			except ValueError:
				tax_rate = float(raw_input("\nValueError: That's not a valid int or float \nInput the tax rate as a decimal. \n(i.e, 10% would be .1, 15% would be .15 or 5% would be .05): "))
				break
			except ZeroDivisionError:
				tax_rate = float(raw_input("\nZeroDivisionError: You can't pick zero! \nInput the tax rate as a decimal. \n(i.e, 10% would be .1, 15% would be .15 or 5% would be .05): "))
				break
			except NameError:
				tax_rate = float(raw_input("\nNameError: That's not a valid int or float. \nInput the tax rate as a decimal. \n(i.e, 10% would be .1, 15% would be .15 or 5% would be .05): "))
				break
			finally:
				print "\nThe tax rate is", tax_rate * 100, "%"
	while True: 
		try:
			tip_rate = sys.argv[3]
		except IndexError:
			try:
				tip_rate = float(raw_input("\nIndexError: It seem sys.argv[3] is out of range. \nLastly, what percent tip do you want to give? \nRepresent as decimal as you did with tax rate: "))
				break
			except ValueError:
				tip_rate = float(raw_input("\nValueError: That's not a valid int or float \nWhat percent tip do you want to give? \nRepresent as decimal as you did with tax rate: "))
				break
			except ZeroDivisionError:
				tip_rate = float(raw_input("\nZeroDivisionError: You can't pick zero! \nWhat percent tip do you want to give? \nRepresent as decimal as you did with tax rate: "))
				break
			except NameError:
				tip_rate = float(raw_input("\nNameError: That's not a valid int or float. \nWhat percent tip do you want to give? \nRepresent as decimal as you did with tax rate: "))
				break
			finally:
				print "\nYou've chosen to give a", tip_rate * 100, "% tip"

	calculate_meal_costs(meal_base, tax_rate, tip_rate)
	print "\nHere's the breakdown for your meal costs: ", calculate_meal_costs(meal_base,tax_rate,tip_rate) 
def main():
	# calculate_meal_costs(argv[])
	base_info = {}
	
	try:
		base_info = dict(base_meal=float(sys.argv[1]), base_tax=float(sys.argv[2]),
					base_tip=float(sys.argv[3]))
		
	except ValueError:
		print "Sorry, you must supply numbers for all imput parameters to this script. Try again."

		while True:
			try:
				base_info['base_meal'] = float(raw_input("Enter base meal cost: ")) 
				base_info['base_tax'] = float(raw_input("Enter base tax percentage as a decimal: "))
				base_info['base_tip'] = float(raw_input("Enter base tip percentage as a decimal: "))
				break
			except ValueError:
				print "Sorry, you must supply numbers for all imput parameters to this script. Try again."

				

	
		
	meal_info = calculate_meal_costs(base_info['base_meal'], base_info['base_tax'],
									base_info['base_tip'])

	print "The base cost of your meal was ${0:.2f}.".format(meal_info['meal_base'])
	print "You need to pay ${0:.2f} for tax.".format(meal_info['tax_value'])
	print "Tipping at a rate of {0}%, you should leave ${1:.2f} for a tip.".format(
    									int(100*meal_info['tip_rate']),
    									meal_info['tax_value'])
	print "The grand total of your meal is ${0:.2f}.".format(meal_info['total'])
def main(argv=sys.argv):
    
    try:
        meal_base = float(sys.argv[1])
        tax_rate = float(sys.argv[2])
        tip_rate = float(sys.argv[3])
    except ValueError:
        print "Sorry, you must supply numbers for all input parameters to this script. Try again."
        meal_base = float(raw_input("Please enter a meal base as a number: "))
        tax_rate = float(raw_input("Please enter a tax rate as a decimal equivalent of a percentage value: "))
        tip_rate = float(raw_input("Please enter a tip rate as a decimal equivalent of a percentage value: "))
    
    meal_info = calculate_meal_costs(meal_base, tax_rate, tip_rate)
    print "The meal base was ${0} with a tax value of ${1} at a tax rate of %{2} and a tip rate of %{3} .  This amounts to ${4}".format("%0.2f" % meal_info["meal_base"], "%0.2f" % meal_info["tax_value"], "%0.0f" % (meal_info["tax_rate"] * 100),"%0.0f" % (meal_info["tip_rate"] * 100), "%0.2f" % meal_info["total"])
Beispiel #4
0
def main(user_input):
	
	meal, tax, tip = user_input[1], user_input[2], user_input[3]
	
	meal = meal_value(meal)
	tax = tax_value(tax)
	tip = tip_value(tip)

	meal_info = calculate_meal_costs(meal, tax, tip)


	print "The base cost of your meal was ${0:.2f}.".format(meal_info['meal_base'])
	print "You need to pay ${0:.2f} for tax.".format(meal_info['meal_base'])
	print "Tipping at a rate of {0}%, you should leave ${1:.2f} for a tip.".format(int(100*meal_info['tip_rate']), 
																					meal_info['tax_value'])

	print "The grand total of your meal is ${0:.2f}.".format(meal_info['total'])
Beispiel #5
0
def main():
    ''' Calculate costs for a meal
    
    The user inputs meal cost, tax rate, and tip rate as parameters
    If any parameters are invalid, the program will
    ask the user for valid ones.
    '''
    
    parser = OptionParser()
    parser.add_option("-m", "--meal", dest="meal", help="base cost of meal")
    parser.add_option("-x", "--tax", dest="tax_percent")
    parser.add_option("-t", "--tip", dest="tip_percent", 
                        help="percent tip you want to leave", default=20)  
                        #let's accrue good karma by defaulting to a decent 20%
                        #tip!
    (options, args) = parser.parse_args()

    #make sure we get valid values
    if not valid_meal_base(options.meal):
        options.meal = get_user_value(valid_meal_base, 
                        'Please enter a valid meal price: ', 
                        'That is not a positive number')
    meal = float(options.meal)
    
    if not valid_percentage(options.tax_percent):
        options.tax_percent = get_user_value(valid_percentage,
                                  'Please enter a valid tax percentage: ',
                                  'Enter a positive number between 0 and 100')
    tax_percent = float(options.tax_percent)
    
    if not valid_percentage(options.tip_percent):
        options.tip_percent = get_user_value(valid_percentage,
                                  'Please enter a valid tip percentage: ',
                                  'Enter a positive number between 0 and 100')
    tip_percent = float(options.tip_percent)
    
    costs = calculate_meal_costs(meal, 0.01*tax_percent, 0.01*tip_percent)
    meal_plus_tax = calculate_rate(0.01*tax_percent, meal) + meal
    tip_amount = costs['total'] - meal_plus_tax
    
    print 'The meal plus tax costs {:.2f} dollars'.format(meal_plus_tax)
    print 'You should tip {:.2f} dollars.'.format(tip_amount)
def main():
	results = {}
	
	for index, k in enumerate(list):
		try:
			results[k] = float(sys.argv[index+1])

		except (ValueError, IndexError):
			print 'Sorry, you must supply numbers for all input parameters to this script. Try again.' 
			
			results[k] = float(raw_input('Please enter meal'+k +':'))

	
	meal_info = calculate_meal_costs(results['cost'], results['tax'] , results["tip"])
	print "The base cost of your meal was ${0:.2f}.".format(meal_info['meal_base'])
	print "You need to pay ${0:.2f} for tax.".format(meal_info['tax_value'])
	print "Tipping at a rate of {0}%, you should leave ${1:.2f} for a tip.".format(
                                        int(100*meal_info['tip_rate']), 
                                        meal_info['tax_value'])
	print "The grand total of your meal is ${0:.2f}.".format(meal_info['total'])