Its not working this is what I am trying to do.
finding out the tax of a person and subtract some based on their status and dependents my expected output is the user will enter its income then will multiply how much tax will be deducted and will also deduct some based on their status and how many dependents
They have:
Income: 500000
Status: Married
Dependents= 3
Tax = 25000
income = float(input("your income: "))
if income <= 100000:
initialIncome = income * .5
elif income in range (100001,250000):
initialIncome = income * .10
elif income in range (250001,500000):
initialIncome = income * .15
elif income >= 500001:
initialIncome = income * .20
status = input("status (s,m,w,d) ")
if status == "S":
S = 10000
elif status == "M":
S = 20000
elif status == "W":
S = 10000
elif status == "D":
S = 30000
dependents = float(input("dependents num: "))
if dependents >= 5:
dependentsPrice = 50000
if dependents <= 5:
dependentsPrice = 10000 * dependents
totalTax = initialIncome - (status + dependents)
print(totalTax)
Code:
You are using status in totalTax instead of S.
You must add .upper() to convert all the input strings to upper case as you have used uppercase in if statements.
income = float(input("your income: "))
if income <= 100000:
initialIncome = income * .5
elif income in range (100001,250000):
initialIncome = income * .10
elif income in range (250001,500001):
initialIncome = income * .15
elif income >= 500001:
initialIncome = income * .20
else:
print("Invalid input")
initaialIncome = 0
# Here you must add upper to convert all the strings to upper case
status = input("status (s,m,w,d) ").upper()
#To prevent undefined error
S =
if status == "S":
S = 10000
elif status == "M":
S = 20000
elif status == "W":
S = 10000
elif status == "D":
S = 30000
#In case user enters wrong input.
else:
print("Invalid input")
dependents = float(input("dependents num: "))
if dependents >= 5:
dependentsPrice = 50000 *dependents
if dependents <= 5:
dependentsPrice = 10000 * dependents
# Here status is string. I suppose you are trying to use S.
#here use dependsPrice
totalTax = initialIncome - (S + dependentsPrice)
print(totalTax)
Related
The code I am trying to write would continuously loop and terminates after a predefined input from the user. It would also do some calculations and print at the end before starting the over. The code that I have written so far below loops, but it does not terminate with the predefined user input. Please help - DC.20212833
salesp_num = ' '
while salesp_num != 00000:
salesp_num = input("Please enter the salesperson's number or enter 00000 to exit:\n")
if len(salesp_num) != 5:
print("The salesperson's number must be 5 digits\n")
else:
salesp_num = int(salesp_num)
salesamount = float(input("Enter the sales amount:\n"))
Class = int(input("What is the class of the sales person? 1, 2, or 3?\n"))
if Class == 1:
if salesamount <= 1000:
commission = salesamount * 0.06
elif salesamount > 1000 and salesamount < 2000:
commission = salesamount * 0.07
elif salesamount > 2000:
commission = salesamount * 0.1
elif Class ==2:
if salesamount < 1000:
commission = salesamount * 0.04
elif salesamount >= 1000:
commission = salesamount * 0.06
elif Class ==3:
commission = salesamount * 0.045
else:
print("Incorrect class")
print(salesp_num)
print(Class)
print(salesamount)
print(commission)
You can change it as below. Note, I have used '0' for exit, not '00000'
salesp_num = ' '
while (salesp_num != '0'):
salesp_num = input("Please enter the salesperson's number or press '0' to exit:\n")
if salesp_num == '0':
print("Exiting....")
break
if len(salesp_num) != 5:
print("The salesperson's number must be 5 digits\n")
else:
salesp_num = int(salesp_num)
salesamount = float(input("Enter the sales amount:\n"))
Class = int(input("What is the class of the sales person? 1, 2, or 3?\n"))
if Class == 1:
if salesamount <= 1000:
commission = salesamount * 0.06
elif salesamount > 1000 and salesamount < 2000:
commission = salesamount * 0.07
elif salesamount > 2000:
commission = salesamount * 0.1
elif Class ==2:
if salesamount < 1000:
commission = salesamount * 0.04
elif salesamount >= 1000:
commission = salesamount * 0.06
elif Class ==3:
commission = salesamount * 0.045
else:
print("Incorrect class")
print(salesp_num)
print(Class)
print(salesamount)
print(commission)
````
I am currently working on improving my commission program by implementing arrays into my program. However, I cannot properly display my commission results anymore. If I revert some array changes, I can display my commission fine. Can someone show me where I did wrong? I would appreciate any feedback on my code to the problem posted below. I'm a beginner and have only included code that I have learned up to this point
MAX = 10
def main():
comp_name = [""] * MAX
sales_amount = [0.0] * MAX
total_sales_amount = 0.0
commission = 0.0
bonus = 0.0
more_sales = 'Y'
select_item = 0
welcome_message()
while more_sales == 'Y':
comp_name[select_item] = get_comp_name()
sales_amount[select_item] = get_sales_amount()
total_sales_amount = total_sales_amount + (sales_amount[select_item] + sales_amount[select_item])
more_sales = more_sales_input()
select_item = select_item + 1
commission += get_commission_calc(sales_amount)
print_receipt(comp_name, sales_amount, select_item)
bonus = get_bonus(commission)
commission = commission + bonus
print_totals(bonus, commission)
def welcome_message():
print("Welcome to your commission calculator (v3)!")
def print_receipt(comp_name, sales_amount, select_item):
sub_total = 0
count = 0
print("\nCompany Name Unit Price Total Price")
print("------------ ---------- -----------")
while count < num_items:
print("{0:<15}".format(comp_name[count]), "\t\t$ ", format(sales_amount[count], ".2f"), "\t$ ", format(sales_amount[count], ".2f"))
sub_total = sub_total + (sales_amount[count])
count = count + 1
print("-----------------------------------------------")
print("Subtotal: $", format(sub_total, ".2f"))
def get_comp_name():
comp = ""
comp = input("\nEnter Company name: ")
return comp
def more_sales_input():
more = ""
more = input("Do you have more sales to add? (y/n): ")
more = more.upper()
while more != "Y" and more!= "N":
print("Invalid entry, either y or n.")
more = input("Do you have more sales to add? (y/n): ")
more = more.upper()
return more
def get_sales_amount():
sales = 0.0
while True:
try:
sales = float(input("Please enter sales $ "))
if sales < 0:
print("Invalid, must be a positive numeric!")
else:
return sales
except:
print("Invalid, must be a positive numeric!")
def get_commission_calc(sales):
commission = 0.0
if sales >= 20000:
commission = sales * .10
elif sales >= 10000:
commission = sales * .07
else:
commission = sales * .05
return commission
def get_bonus(commission):
if commission >= 1000:
return + 500
else:
return 0
def print_totals(bonus, total_commission):
if bonus > 0:
print("\nYou earned a $500 bonus added to your pay!")
else:
print("\nYou did not yet meet requirements for a bonus!")
print("\nYour commission is", '${:,.2f}'.format(total_commission))
main()
You're passing 'sales_amount', which is a list, to 'get_commission_calc', which then compares the list with a number; hence the error.
What I believe you're trying to do is not passing the list, but the 'selected_item' of the list.
If that's the case, the function call should look more like this:
get_commission_calc(sales_amount[selected_item])
I am solving the following problem, and came across the situation where I'm unable to define either decision operators or the while loop properly.
The task:
Customers get a monthly discount depending on the length of contract they take out as shown below:
3 – 6 months 2% discount
7 – 12 months 5% discount
over 12 months 10% discount
The program should ask users for their name and the monthly cost of their game package. It should then ask them to enter the contract length they would like. The maximum contract length is 18 months. The program should finally display the entered details and the final cost of the package with the discount applied.
contract_length = 0
final_cost = 0
#prompt the user to enter their name
user_name = input("What is your name? ")
#prompt the user to enter monthly cost for the game package
package_cost = float(input("Monthly cost of your game package: "))
while contract_length > 0 and contract_length <= 18:
#prompt the user to enter the contract length
contract_length = int(input("Enter the contract length you would like: "))
#selection statement to calculate final cost
if contract_length > 12:
discount = (package_cost * 0.1)
final_cost = format(package_cost - discount,".2f")
elif contract_length > 6:
discount = (package_cost * 0.05)
final_cost = format(package_cost - discount, ".2f")
elif contract_length >= 3:
discount = (package_cost * 0.02)
final_cost = format(package_cost - discount, ".2f")
else:
print("Invalid entry")
#display results
print("Name " +user_name)
print("Package Cost £",str(package_cost))
print("Months in Contract " , str(contract_length))
print("The final cost is £",str(final_cost))
Error I am having
What is your name? Jhon
Monthly cost of your game package: 35.12
Name Jhon
Package Cost £ 35.12
Months in Contract 0
The final cost is £ 0
>>>
I even tried "and" to "or" but with or operator it's start repeating the input function under while loop. Any help? Thanks.
I would recommend you to have a condition on the final_cost, like this:
contract_length = 0
final_cost = 'none'
#...
while final_cost == 'none':
#prompt the user to enter the contract length
contract_length = int(input("Enter the contract length you would like: "))
if contract_length > 18:
print("Invalid entry")
continue
#selection statement to calculate final cost
if contract_length > 12:
discount = (package_cost * 0.1)
final_cost = format(package_cost - discount,".2f")
elif contract_length > 6:
discount = (package_cost * 0.05)
final_cost = format(package_cost - discount, ".2f")
elif contract_length >= 3:
discount = (package_cost * 0.02)
final_cost = format(package_cost - discount, ".2f")
else:
print("Invalid entry")
Here is another solution:
contract_length = 0
final_cost = 0
package_cost = 0
user_name = None
while not user_name:
user_name = input("What is your name? ")
while package_cost <= 0:
package_cost = input("Monthly cost of your game package: ")
if not package_cost.isdigit():
package_cost = 0
package_cost = float(package_cost)
while not (3 <= contract_length <= 18):
contract_length = input("Enter the contract length you would like: ")
if not contract_length.isdigit():
contract_length = 0
contract_length = int(contract_length)
if contract_length > 12:
discount = 0.1
elif contract_length > 6:
discount = 0.05
elif contract_length >= 3:
discount = 0.02
else:
discount = 0
final_cost = package_cost - (package_cost * discount)
print("Name", user_name)
print("Package Cost £", format(package_cost, ".2f"))
print("Months in Contract", contract_length)
print("The final cost is £", format(final_cost, ".2f"))
I have this code to calculate tax. I am having an syntax error like this NameError: name 'raw_input' is not defined and don't know why? I am quite new to coding and have little understanding about the subject. Like it takes me forever to write few lines and understand what is what after some long research.
I wrote something and it gives me an error, and I am not really sure about my error. Like I did something similar after an agonizing time later, and float() did the trick with it. Not sure what I am missing?
P.S: I read the how to upload the code properly for people to approach it, like minimal version. I don't think I quite grasped what it wants me to do. Sorry if that is violated a rule or made it harder to read!
# input of tax status
tax_status = raw_input('Enter your tax status(single or married) : ')
# validate tax status
while tax_status.strip().lower() != 'single' and tax_status.strip().lower() != 'married':
print('Invalid value. Tax status can be eiher single or married')
tax_status = raw_input('Enter your tax status(single or married) : ')
#input of income
income = float(raw_input('Enter your income: '))
# validate income > 0
while income <= 0 :
print('Invalid value. Income must be greater than 0')
income = float(raw_input('Enter your income: '))
tax_amount = 0
# calculate tax amount based on tax_status and income
if tax_status == 'single':
if income <= 9700:
tax_amount = (10*income)/100
elif income <= 39475:
tax_amount = (12*income)/100
elif income <= 84200:
tax_amount = (22*income)/100
elif income <=160725:
tax_amount = (24*income)/100
elif income <= 204100:
tax_amount = (32*income)/100
elif income <= 510300:
tax_amount = (35*income)/100
else:
tax_amount = (37*income)/100
else:
if income <= 19400:
tax_amount = (10*income)/100
elif income <= 78950:
tax_amount = (12*income)/100
elif income <= 168400:
tax_amount = (22*income)/100
elif income <=321450:
tax_amount = (24*income)/100
elif income <= 408200:
tax_amount = (32*income)/100
elif income <= 612350:
tax_amount = (35*income)/100
else:
tax_amount = (37*income)/100
# output the tax amount
print('Your tax amount is $%.2f' %(tax_amount))
I at least want to know what I did wrong, and how can I figure out to make it run? Like what I missed that it causes me this issue, that when I try to run it doesn't?
Here is a cleaner way of writing that should work using some of the standard conventions in python. The specific error you were encountering is because raw_input is python2 and was replaced with input.
income = int(input('Enter your income: '))
tax_status = input('Enter your tax status(single or married) : ').lower()
if tax_status == 'single':
SingleIncomeLevel = {10:9700, 12:39475, 22:84200, 24:160725, 32:204100, 35:510300}
for multiplier in SingleIncomeLevel:
if income <= SingleIncomeLevel[multiplier]:
tax_amount = (multiplier*income)/100
else:
tax_amount = (37*income)/100
else:
marriedIncomeLevel = {10:19400, 12:78950, 22:168400, 24:321450, 32:408200, 35:612350}
for multiplier in marriedIncomeLevel:
if income <= marriedIncomeLevel[multiplier]:
tax_amount = (multiplier*income)/100
else:
tax_amount = (37*income)/100
print(f"Your tax amount is {tax_amount}")
I need to calculate shipping costs based on package type, weight, and how many zones the delivery goes through. I didn't get all the hard numbers from him so I'm using some placements, but that shouldn't matter much. The problem is that even though no errors are listed, running the program only returns a blank page, no prompts to enter numbers or anything like that.
Here's the code.
def main():
packageType = input('Please enter the package type: ')
rate = 0
zoneRate = 0
if packageType == 1:
rate += 1.25
elif packageType == 2:
rate += 1.5
elif packageType == 3:
rate += 1.75
elif packageType == 4:
rate += 2
weight = input('Please enter the weight: ')
if weight <= 2:
rate += 3.10
elif weight > 2 and weight <= 6:
rate += 4.20
elif weight > 6 and weight <= 10:
rate += 5.30
elif weight > 10:
rate += 6.40
zones = input('Please enter how many zones are crossed: ')
if zones == 1:
zoneRate += 5
if zones == 2:
zoneRate += 10
if zones == 3:
zoneRate += 15
cost = rate * zoneRate
print(('The shipping cost is: '), cost)
You need to have
def main():
# all of your code
# goes here
main()