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):
Related
# Main to run
def main():
keyboardInput = int(input('Enter the price of the land: $')) # grab user input
calculate(keyboardInput) # function to calculate w/ kbInput parameter
display(assessmentValue, realTax) # display the values <- this one right here
# Calculate the assessment value and property tax. Display afterwards
def calculate(keyboardInput):
assesssmentValue = keyboardInput * 0.60 # 60% for property actual value
realTax = (assesssmentValue * .64) / 100 # calculate the tax
return assesssmentValue, realTax
# Display the prices
def display(assesssmentValue, realTax):
print('The assessment value is : $', format(assesssmentValue, '.2f'))
print('The property tax is: $', format(realTax, '.2f'))
# ^ display the values
# Call the main to run
main()
Overall things I've done so far:
tried to make variables for them, but I am also trying to figure out also how to grab the returning values from calculate function, if that was doable.
delete and revise the code, it works the method I tried, but I want to learn on this portion with which im stuck with.
everything so far from what I've seen works besides the option to display the function for additional info and context
Issue: display the "display" function from the main method
ps. sorry for the bad formatting of my post, new to here
EDIT: fixed typo in code
Just assign the value from the calculate() call to your variables, on the outside.
def main():
keyboardInput = int(input('Enter the price of the land: $'))
assesssmentValue, realTax = calculate(keyboardInput)
display(assesssmentValue, realTax)
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)
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 )))
I'm using python for the very first time and I am stuck on this stinking problem and cant for the life of me figure out why its not working. When I try and run my program I can get an answer for the yearly cost without the modification (even though its wrong and I dont know why) but not the yearly cost with the modification.
I've tried rewriting it in case I missed a colon/parenthesis/ect but that didnt work, I tried renaming it. And I tried taking it completely out (this is the only way I could get rid of that annoying error message)
payoff file
from mpg import *
def main():
driven,costg,costm,mpgbm,mpgam = getInfo(1,2,3,4,5)
print("The number of miles driven in a year is",driven)
print("The cost of gas is",costg)
print("The cost of the modification is",costm)
print("The MPG of the car before the modification is",mpgbm)
print("The MPG of the car afrer the modification is",mpgam)
costWithout = getYearlyCost(1,2)
print("Yearly cost without the modification:", costWithout)
costWith = getYearlyCost2()
print("Yearly cost with the modification:", costWith)
While I know there is an error (most likely a lot of errors) in this I cant see it. Could someone please point it out to me and help me fix it?
Also I added my mpg.py in case the error is in there and not the payoff file.
def getInfo(driven,costg,costm,mpgbm,mpgam):
driven = eval(input("enter number of miles driven per year: "))
costg = eval(input("enter cost of a gallon of gas: "))
costm = eval(input("enter the cost of modification: "))
mpgbm = eval(input("eneter MPG of the car before the modification: "))
mpgam = eval(input("enter MPG of the car after the modification: "))
return driven,costg,costm,mpgbm,mpgam
def getYearlyCost(driven,costg):
getYearlyCost = (driven / costg*12)
def getYealyCost2(driven,costm):
getYearlyCost2 = (driven / costm*12)
return getYearlyCost,getYearlyCost2
def gallons(x,y,z,x2,y2,z2):
x = (driven/mpgbm) # x= how many gallons are used in a year
y = costg
z = (x*y) # z = how much money is spent on gas in year
print("money spent on gas in year ",z)
x2 = (driven/mpgam) # x2 = how much money is spent with mod.
z2 = (x2*y)
y2 = (costm + z2)
1,1 Top
Here's your immediate problem:
costWith = getYearlyCost2()
The function you're trying to call is named getYealyCost2() (no "r").
There are other problems that you'll find as soon as you fix that, such as no return statement in getYearlyCost() and trying to return the function getYearlyCost() in getYearlyCost2() and calling getYearlyCost2() without any arguments.
On top of that, import * is frowned upon, and then there's the use of eval()... but that'll do for starters.
Hi I got the following problem, I'm trying to build a program that reads a txt file that contains lines with name of fruits and their respective base prices as the following example:
apple $ 2.00
pearl $ 4.00
guava $ 2.50
and so it goes on.
Yesterday I had some help with people here and I've learned how to make python recognize their prices as a number instead of string, and i was able to multiply their values by one number that was asked to the user. Now I want to make this in a such way that I'm able to take each one of those prices and multiply them for different numbers (one different number for each price) that will be asked for the user to choose.
So far, with the help that I got yesterday I've done this:
print "this program will calculate the total price of the fruits"
y = input('insert a value = ')
with open('fruits.txt') as f:
for line in f:
name, price = line.rstrip().split('$')
price = float(price)
cost = price * (0.76+y)
tcost = cost + price
print name, tcost
Any ideas how to do this?
mult_by = int(raw_input('gimme a number for something: '))
try and familiarize yourself with the basic built in functions, should be helpful.
http://docs.python.org/library/functions.html