Ok so, I have an assignment to make a carsales program which is suppose to calculate how much the salesperson will make in a week. I already know how much all the cars sell for and how much commission he makes. Here is my code:
def main():
print ('This program will compute the comission earned for the week based on your sales for the week.')
car_number = float(input('Enter number of cars sold :'))
def calculate_total(car_number,price,commission_rate):
price = 32,500.00
commission_rate = .025
calculate_total = car_number * price * commission_rate
return calculate_total(car_number)
print('The weekly gross pay is $',calculate_total)
main()
The program isn't working for some reason but I decided to submit it to my professor anyway. He then replied by saying that I wasn't asked to create a new function and that I have to delete it and work just in main. Can someone please tell me what this means?
Two things:
'Working in main' as your professor said means that you don't define any functions. All your code just sits in the file, without any def ... statements. I know that's probably not clear. Here's an example:
import os
print "Your current working directory is:"
print os.getcwd()
This kind of programming has more the feel of a 'script' - you're not defining parts of the program that you're going to use more than once, and you're not taking the trouble to break down what the program does into single-purpose functions.
Second, you've entered price in such a way that Python thinks you're creating a tuple of numbers instead of a single value.
price = 32,500.00 is interpreted by Python as creating a tuple, with values 32 and 500.00 in it. What you actually want is: price = 32500.00.
I broke down and completed the process for you.
print ('This program will compute the comission earned for the week based on your sales for the week.')
car_number = float(input('Enter number of cars sold :'))
price = 32500.00
commission_rate = .025
calculate_total = car_number * price * commission_rate
print('The weekly gross pay is $',calculate_total)
Sorry i did not saw the complete question before but anyway this is the correct answer without a function
The keywords try and except are for error handling. If you give as input something invalid let's say a letter instead of number will throw a message
(Could not convert input data to a float.)
def main():
print ('This program will compute the comission earned for the week based on your sales for the week.')
try:
#before: car_number = float(raw_input('Enter number of cars sold :'))
car_number = float(input('Enter number of cars sold :'))
except ValueError:
#before: print 'Could not convert input data to a float.'
print('Could not convert input data to a float.')
print('The weekly gross pay is ${}'.format(car_number * 32500.00 * 0.025 )))
main()
If you don't even want main() function here is the answer:
print ('This program will compute the comission earned for the week based on your sales for the week.')
try:
car_number = float(input('Enter number of cars sold :'))
except ValueError:
print('Could not convert input data to a float.')
print('The weekly gross pay is ${}'.format(car_number * 32500.00 * 0.025 )))
Related
I'm writing a program that will greet somebody (given name) and then ask them how many hours they worked and how much their hourly pay is. The program runs but is not spitting back the correct math. I'm getting this for my answer... you earned<function wage at 0x00EA9540>
I have already tried calling payment but not getting an answer with that either.
def greet(greeting):
name = input("Hi, whats your name?")
return greeting + name
print(greet ("Hey "))
hourly = input("How much is your hourly wage?")
hours = input("How many hours did you work this week?")
def wage(hourly, hours):
if hours > 40:
payment = 40 * hourly
payment = payment + hourly * (hours-40) * 1.5
return payment
else:
return hours * hourly
print("you earned" + str(wage))
You missed the parameters to the wage function.
in your case, it just prints the memory address of the function wage...
you need to change the print call with the correct parameters to the wage function:
print("you earned" + str(wage(hourly, hours)))
You need to call the wage function with the parameters:
print("you earned" + str(wage(hourly, hours)))
Otherwise you are simply printing the string representation of the wage function object, and that doesn't really make much sense.
I'm in an intro programming class and am lost. We've had several labs that required knowledge that we haven't been taught but I've managed to find out what I need on google (as nobody responds to the class message board) but this one has me pretty frustrated. I'll include a pastebin link here: https://pastebin.com/6JBD6NNA
`principal = input()
print('Enter the Principal Value of your investment: $', float(principal))
time = input()
print('\nEnter the Time(in years) you plan to save your investment: ', int(time))
rate = input()
print('\nEnter the Rate (2% = 0.02) you will collect on your investment: ', float(rate))
interest = (float(principal) * float(rate)) * int(time)
final_value = float(principal) + float(interest)
print('\nThe Final Value of your investment will be: $%.2f' % final_value)`
So I need the output of the dollar amounts to have a comma ($27,500.00) but I have no idea how to do this. I've seen a couple of solutions on this site and others but I can't get them to work. PLEASE can someone help me?
In Python 2.7 or above, you can use
print('The Final Value of your investment will be: ${:,.2f}'.format(final_value))
This is documented in PEP 378.
Source: Python Add Comma Into Number String
Here is a working example:
principal = float(input('Enter the Principal Value of your investment: $'))
time = int(input('\nEnter the Time(in years) you plan to save your investment: '))
rate = float(input('\nEnter the Rate (2% = 0.02) you will collect on your investment: '))
interest = principal * rate * time
final_value = principal + interest
print('The Final Value of your investment will be: ${:,.2f}'.format(final_value))
Your last line should be:
print ("\nThe Final Value of your investment will be: ${:,.2f}".format(final_value))
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
tip and tax calculator
bill = price + tax + tip
price = raw_input("What is the price of the meal")
tax = price * .06
tip = price * 0.20
what is wrong with my code
I have tried everything
please answer and get back to me
a few things.
bill = price + tax + tip #You can't add up these values BEFORE calculating them
price = raw_input("What is the price of the meal") #raw_input returns a string, not a float which you will need for processing
tax = price * .06 #So here you need to do float(price) * .06
tip = price * 0.20 #And float(price) here as well.
#Then your " bill = price + tax + tip " step goes here
First of all, you can't use variables that you haven't defined: in your code your are using bill = price + tax + tip but your program doesn't even know what price, tax and tip are yet, so that line should be at the end of the code, after you've asked the price and calculated tax and tip.
Then, you have raw_input, this function returns a string, if you want to convert it to a decimal number that you can multiply and add (float) you can use price = float(raw_input("what is the price of the meal"))
Correct that two things and it should work...
Heres a couple of things wrong with the code:
You're trying to calculate the total before some variables have been defined.
The raw_input function returns a string so you can't do proper mathematical calculations before you coerce it into an integer.
In calculate the tips/tax you should use a float with the whole number 1(1.20) to take the whole value of the bill + 20%.
Below is a code snippet that should work how you want and give you something to think about on how to pass dynamic values into the modifiers within the calculate_bill function for custom tip floats and custom tax floats:
def calculate_bill(bill, bill_modifiers):
for modifier in bill_modifiers:
bill = modifier(bill)
return bill
def calculate_tip(bill, percentage=1.20):
return bill * percentage
def calculate_tax(bill, percentage=1.06):
return bill * percentage
if __name__ == '__main__':
bill = int(input("What is the price of the meal: "))
total_bill = calculate_bill(bill, [calculate_tip, calculate_tax])
print(total_bill)
So far I have been coding this all week trying to get it to work.
It should come out as this:
Please enter Amount that you would like to borrow(£): 4000
Please enter Duration of the loan(Years):2
Please enter the interest rate (%):6
The total amount of interest for 2 (years) is: £480.00
The total amount of the period for 2 (years) is £4480.00
You will pay £186.67 per month for 24 months.
Do you wish to calculate a new loan payment(Y or N)
Code:
monthlypayment = 0 #Variable
loanamount = 0 #Variable
interestrate = 0 #Variable
numberofpayments = 0 #Variable
loandurationinyears = 0 #Variable
loanamount = input("Please enter the amount that you would like to borrow(£) ")
loandurationinyears = input("How many years will it take you to pay off the loan? ")
interestrate = input("What is the interest rate on the loan? ")
#Convert the strings into floating numbers
loandurationinyears = float(loandurationinyears)
loanamount = float(loanamount)
interestrate = float(interestrate)
#Since payments are once per month, number of payments is number of years for the loan
numberofpayments = loandurationinyears*12
#calculate the monthly payment based on the formula
monthlypayment = (loanamount * interestrate * (1+ interestrate)
* numberofpayments / ((1 + interestrate) * numberofpayments -1))
#Result to the program
print("Your monthly payment will be " + str(monthlypayment))
looks to me like the only thing you're missing in the code above from what you described is a while loop. This will allow your program to calculate the loan and run the program over and over again until the user inputs no, in which case the program exits. all what you have to do is:
YorNo = input("Do you wish to calculate a loan payment")
YorNo.Title()
while YorNo != "n":
#Your code goes here
YorNo = input("Do you wish to calculate a loan payment")
YorNo.Title()
print("Thank you for using the program")
In case you dont understand this, baisically, you type the first 3 lines just before your code. Then you leave an indentation and type your code after them. Once your done, you type in the 3rd and 4th line. Then, simply go back that indentation (to show the program that this isnt part of the loop.) If im not mistaken, the result of this will be:
You will be asked wether you want to calculate a loan
If you answer "y" your code will run and the loan will be calculated and printed to the user
Then you will be asked again. The above will repeat until you input "n"
the N cant be capitalised
Hello I'm a college student whose just begun to take a computer programming class and have hit a bit of a snag I'm struggling to figure out. The program I'm supposed to create using python is a car sales program that calculates the weekly gross pay that is the total of made from the cars sold in the week. I have to input the number of cars sold and the value given back should be how much was made that week, this is the code I have written :
def main():
car_number = float(input('enter number cars sold'))
def calculate_total('car_number,price,commission'):
price = 32,500.00
commission = .025
total = car_number * price * commission
return total
main()
on the line 'def calculate_total('car_number, price, commission'): I am receiving a syntax error declaring 'formal parameter expected' How do I fix this issue?
That line should be
def calculate_total(car_number,price,commission):
You are instead writing a string there.
Also, as you are changing the values of price and commission it is better defined as
def calculate_total(car_number):
You have forgotten to call the function at the end
return calculate_total(car_number)
def calculate_total('car_number,price,commission'): this line should be
def calculate_total(car_number):