I am brand new to coding so I hope this is a small mistake. Below is the code for an assignment on a paper carrier's salary. I get no error codes but no output, even the print functions do not show. Please help
# This program will calculate the weekly pay of a paper carrier.
# Developer: Hannah Ploeger Date: 08/30/2022
def main():
# initialize variables
paperCost = 4
commission = 0.05
# prompt user for number of papers
numPaper = eval(input("How many papers are delivered on your route?"))
# prompt user for days of delivery
numDays = eval(input("How many days was the paper delivered this week"))
# prompt user for tips recieved
numTips = input("How much did you recieve in tips this week?")
# calculate salary
weekPay = ((numPaper * numDays) * paperCost) * commission
totalPay = weekPay + numTips
# display output
print("This week you delivered", numPaper, "papers")
print("Your salary this week is $", weekPay)
print("Your total tips were $", numTips)
print("Your total pay is $", totalPay)
enter image description here
add this:
if __name__ == "__main__":
main()
just call the main function
def main():
# initialize variables
paperCost = 4
commission = 0.05
# prompt user for number of papers
numPaper = eval(input("How many papers are delivered on your route?"))
# prompt user for days of delivery
numDays = eval(input("How many days was the paper delivered this week"))
# prompt user for tips recieved
numTips = input("How much did you recieve in tips this week?")
# calculate salary
weekPay = ((numPaper * numDays) * paperCost) * commission
totalPay = weekPay + numTips
# display output
print("This week you delivered", numPaper, "papers")
print("Your salary this week is $", weekPay)
print("Your total tips were $", numTips)
print("Your total pay is $", totalPay)
main() # calling main function
Related
I have created a simple python program that lets users drive different cars. The user will enter their full name, address, and phone number. The user will then be asked which car they wish to drive, with a maximum of five selected cars. The cars have set prices and a total bill will be added up at the end of the program, however, I am currently unable to find a solution to work out the total cost for the user. The program also asks the user how many laps of the race they wish to perform in the car, I have already worked out how to display the total cost of the laps, but need it added to the total cost somehow. Thanks!
Code
cars_list = ["Lamborghini", "Ferrari", "Porsche", "Audi", "BMW"]
cars_prices = {"Lamborghini":50, "Ferrari":45, "Porsche":45, "Audi":30, "BMW":30}
laps_cost = 30
final_cost = []
final_order = {}
cust_num_cars = int(input("Please enter the number of cars you want to drive in this session: "))
while cust_num_cars > 5:
cust_num_cars = int(input("You can only drive a maximum of five cars! Please try again.\n Enter cars: "))
for index in range(cust_num_cars):
print("\nChoose a car type from the following list", cars_list)
select_cars = input("Select a car type: ")
if select_cars in cars_list:
print("\nYou have selected to drive the", {select_cars})
final_cost.append(cars_prices[select_cars])
if select_cars not in cars_list:
print("\n",{select_cars}, "is not in the available list of cars to drive!")
cust_name = input("\nPlease enter your full name: ")
cust_address = input("Please enter your full address: ")
cust_phone_num = int(input("Please enter your mobile phone number: "))
add_laps = input("\nWould you like to drive additional laps? (Yes/No): ")
if add_laps == "Yes":
print("\nYou have selected an additional lap!")
num_of_laps = int(input("Number of additional laps: "))
print("\nYou have selected", num_of_laps, "additional laps!")
final_cost.append(cars_prices[add_laps])
sum = num_of_laps * laps_cost
else:
print("\nYou have selected no additional extra laps.")
print("Total laps cost: £",final_cost)
print("\n----Order & Billing summary----")
print("Customer Full Name:", cust_name)
print("Customer Address:", cust_address)
print("Customer Phone Number", cust_phone_num)
print("Total cost", final_cost.append(cars_prices))
I have tried everything I know in my little experience with Python to work out a final cost. I have worked out the total cost for the number of laps, but can't work out how to add that to the cost of the selected cars and then display a total cost.
First, you can't use a for-loop because when you select a car that isn't in the possibilities you have lost an iteration, use a while loop
# final_cost renamed to car_cost
while len(car_cost) != cust_num_cars:
print("\nChoose a car type from the following list", cars_list)
select_cars = input("Select a car type: ")
if select_cars in cars_list:
print("\nYou have selected to drive the", select_cars)
car_cost.append(cars_prices[select_cars])
else:
print("\n", select_cars, "is not in the available list of cars to drive!")
Then use sum() for the cars cost and add to the laps cost
num_of_laps = 0
add_laps = input("\nWould you like to drive additional laps? (Yes/No): ")
if add_laps == "Yes":
print("\nYou have selected an additional lap!")
num_of_laps = int(input("Number of additional laps: "))
print("\nYou have selected", num_of_laps, "additional laps!")
else:
print("\nYou have selected no additional extra laps.")
total = num_of_laps * laps_cost + sum(car_cost)
print("Total price:", total)
To calculate the total cost, you can use the built-in sum() function, which takes an iterable (like a list) and returns the sum of all its elements. In your case, you can use it to add up all the prices of the cars that the user selected:
total_cost = sum(final_cost)
Then you can add the cost of the laps to the total cost. In your code, the variable sum holds the total cost of the laps. You can add that to the total_cost variable:
total_cost += sum
Hi friend in this program its better to add all of your costs in one variable.
in a list your list's elements are not adding together,
but with variables you can add your cost every time you get a cost like this:
cost_sum = 0
cost = input("Enter a cost: ")
cost_sum += cost
here is the code=
def main():
print("Enter your age: ")
age= float(input())
while age >= 0:
if (age<= 2.00) :
print("The guest whose age is",age,"is admitted without charge.")
elif ((age>= 3.00) and (age<= 12.00)):
print("A(n)",age," year old will cost $14.00 dollars for admission.")
elif (age>=65.00) :
print("A(n)",age,"year old will cost $18.00 dollars for admission.")
else :
print("A(n)",age,"year old will cost $23.00 dollars for admission.")
print("End of guest list")
main()
and here is the problem I am trying to solve:
Create a program that begins by reading the ages of all of the guests in a group from the user, with one age entered on each line. The user will enter -1 to indicate that there are no more guests in the group. Then your program should display the admission cost for the group with an appropriate message. The cost should be displayed using two decimal places. Use the following sample run for input and output messages.
You need to move the prompt for input into the loop, otherwise age never changes within the while, creating an infinite loop.
def main():
age = 1
while age >= 0:
print("Enter your age: ")
age = float(input())
if (age<= 2.00) :
print("The guest whose age is",age,"is admitted without charge.")
elif ((age>= 3.00) and (age<= 12.00)):
print("A(n)",age," year old will cost $14.00 dollars for admission.")
elif (age>=65.00) :
print("A(n)",age,"year old will cost $18.00 dollars for admission.")
else :
print("A(n)",age,"year old will cost $23.00 dollars for admission.")
print("End of guest list")
main()
You never update age after its initial value and so the while loop continues forever, printing a line for each iteration.
You need to put a
age = float(input())
as the last line of the while loop.
I honestly have no idea what I'm doing but, I know as soon as I get a bit of help, it'll come to me. So, here's the code and I know I've done something wrong? Won't let me complete the input?
"""
WeeklyPay.py: generate payroll stubs for all hourly paid employees and summarizes them
"""
def main():
"""
WeeklyPay.py generates and prints and employee pay stubs based off hours_worked, hourly_rate, and gross_pay
:return: None
"""
total_gross_pay = 0
hours_worked = 0
more_employee = input()
more_employee = input("Did the employee work this week? Y or y for yes: ")
while more_employee == "Y" or "y":
hours_worked = input("How many hours did the employee work? ")
hourly_rate = input("Please enter the employee's hourly rate: ")
I am doing a project for class and can't figure out where I am going wrong. The code works in sections, but when I run it all together it shuts down right after the first input.
I think I need to call functions somewhere - but how and where?
Below is all my code so far, with comments.
import sys
#account balance
account_balance = float(500.25)
##prints current account balance
def printbalance():
print('Your current balance: %2g'% account_balance)
#for deposits
def deposit():
#user inputs amount to deposit
deposit_amount = float(input())
#sum of current balance plus deposit
balance = account_balance + deposit_amount
# prints customer deposit amount and balance afterwards
print('Deposit was $%.2f, current balance is $%2g' %(deposit_amount,
balance))
#for withdrawals
def withdraw():
#user inputs amount to withdraw
withdraw_amount = float(input())
#message to display if user attempts to withdraw more than they have
if(withdraw_amount > account_balance):
print('$%.2f is greater than your account balance of $%.2f\n' %
(withdraw_amount, account_balance))
else:
#current balance minus withdrawal amount
balance = account_balance - withdraw_amount
# prints customer withdrawal amount and balance afterwards
print('Withdrawal amount was $%.2f, current balance is $%.2f' %
(withdraw_amount, balance))
#system prompt asking the user what they would like to do
userchoice = input ('What would you like to do? D for Deposit, W for
Withdraw, B for Balance\n')
if (userchoice == 'D'): #deposit
print('How much would you like to deposit today?')
deposit()
elif userchoice == 'W': #withdraw
print ('How much would you like to withdraw today?')
elif userchoice == 'B': #balance
printbalance()
else:
print('Thank you for banking with us.')
sys.exit()
This part should be as one userchoice = input ('What would you like to do? D for Deposit, W for Withdraw, B for Balance\n')
Not sure if you indented by accident, but python does not like that.
Also, advice for your code. Make it so user can either do uppercase or lowercase letters for input, also make sure it still grab input even if user put empty spaces after input string.
Your withdraw exit the program after entering the string character W.
Balance is not grabbing the correct Deposit.
Use for loops and condition to keep it looping and ask user when to exit.
I am writing a code in python where I use different functions to calculate wage, federal tax, state tax, and net worth. Everything is fine except my output says, ('Your wage is: $', 989) instead of Your wage is: $989 I tried using +(wag) but it doesn't let me run it. How do I get rid of the parenthesis, comma, and quotation marks from the output? And how do I make the output have no decimal points? I am not using float, so I don't know why it's still giving me decimal points. Here's my code:
def Calculatewages():
wage = (work*hours)
return wage
def CalcualteFederaltax():
if (status== ("Married")):
federaltax = 0.20
elif (status== ("Single")):
federaltax = 0.25
elif status:
federaltax = 0.22
federaltax = float(wag*federaltax)
return federaltax
def Calculatestatetax():
if(state=="CA") or (state=="NV") or (state=="SD") or (state=="WA") or (state=="AZ"):
statetax = 0.08
if (state== "TX") or(state=="IL") or (state=="MO") or (state=="OH") or (state=="VA"):
statetax = 0.07
if (state== "NM") or (state== "OR") or (state=="IN"):
statetax = 0.06
if (state):
statetax = 0.05
statetax = float(wag*statetax)
return statetax
def Calculatenet():
net = float(wag-FederalTax-StateTax)
return net
hours = input("Please enter your work hours: ")
work = input("Please enter your hourly rate: ")
state = input("Please enter your state of resident: ")
status = input("Please enter your marital status: ")
print("**********")
wag = Calculatewages()
FederalTax = CalcualteFederaltax()
StateTax = Calculatestatetax()
Net = Calculatenet()
print("Your wage is: $" ,wag)
print("Your federal tax is: $",FederalTax)
print("Your state tax is: $",StateTax)
print("Your net wage is: $",Net)
For this part:
print("Your wage is: $" ,wag)
print("Your federal tax is: $",FederalTax)
print("Your state tax is: $",StateTax)
print("Your net wage is: $",Net)
you can rewrite it as this using string's format() method:
print("Your wage is: ${}".format(wag))
print("Your federal tax is: ${}".format(FederalTax))
print("Your state tax is: ${}".format(StateTax))
print("Your net wage is: ${}".format(Net))
This is useful as you can insert the value into any place in the string (wherever you put the curly brackets).
As for your decimal points problem you can use the built in round function like this:
round(float(variable), int(decimal_places))
for example:
round(1.43523556, 2)
will return 1.44
There are no quotes or parenthesis in the output in python 3.x, Check if you are running on python 2 or python 3. Looks like you are on python 2 by judging your output.
So change all your print statements like this
print "Your net wage is: $", wag # remove brackets
...
However, if you want it to run on python 3 your code doesn't run as you are multiplying 2 strings in this line
def Calculatewages():
wage = (work*hours) # <------ here
return wage
To fix this issue you must cast them into int and then your code should run without problems.
hours = int(input("Please enter your work hours: ")) # < ---- Cast to int
work = int(input("Please enter your hourly rate: ")) # < ---- Cast to int
state = input("Please enter your state of resident: ")
status = input("Please enter your marital status: ")
My output:
Please enter your work hours: 8
Please enter your hourly rate: 10
Please enter your state of resident: IN
Please enter your marital status: Single
**********
Your wage is: $ 80
Your federal tax is: $ 20.0
Your state tax is: $ 4.0
Your net wage is: $ 56.0
you can also use string's format() method:
print("Your wage is: ${}".format(wag))
print("Your federal tax is: ${}".format(FederalTax))
print("Your state tax is: ${}".format(StateTax))
print("Your net wage is: ${}".format(Net))
If you're running under python3.x, then with your code, it should print out without the parenthesis, and if you're running under python2.x, then to get rid of the parenthesis, you might wanna try:
print "Your wage is: $", wag