This is my current code for a tax calculator based on age. I think that it would be easier to update in the future if I used data structures when calculating the brackets. Could someone help me make sense of this?
while True: #Loop the whole program
from datetime import datetime, date #Get the Age of from user input
print("Please enter your date of birth (dd mm yyyy)")
date_of_birth = datetime.strptime(input("--->"), "%d %m %Y")
def calculate_age(born):
today = date.today()
return today.year - born.year - ((today.month, today.day) < (born.month, born.day))
age = calculate_age(date_of_birth)
print("You are " ,int(age), " years old.")
#Get the Salary from user input
def get_salary():
while True:
try:
salary = int(input("Please enter your salary: "))
except ValueError:
print("You need to enter a number ")
else:
break
return salary
#Calculate the Amount that needs to be paid, using if,elif,else
def contribution(age):
if age <= 35:
tax = (salary * 0.20)
elif 36 <= age <= 50:
tax = (salary * 0.20)
elif 51 <= age <= 55:
tax = (salary * 0.185)
elif 56 <= age <= 60:
tax = (salary * 0.13)
elif 61 <= age <= 65:
tax = (salary * 0.075)
else:
tax = (salary * 0.05)
return tax
#Print the amount
if __name__ == "__main__": # It's as if the interpreter inserts this at the top of your module when run as the main program.
salary = get_salary() #get salary from get_salary()
tax = contribution(age) #calculate the tax
print("you have to pay", tax, " every month ")
while True:
answer = str(input("Do you need to do another calculation? (y/n): "))
if answer in ("y", "n"):
break
print ("invalid input.")
if answer == "y":
continue
else:
print("Thank you for using this Calculator, Goodbye")
break
So the code that I'm assuming that I need to change is:
#Calculate the Amount that needs to be paid, using if,elif,else
def contribution(age):
if age <= 35:
tax = (salary * 0.20)
elif 36 <= age <= 50:
tax = (salary * 0.20)
elif 51 <= age <= 55:
tax = (salary * 0.185)
elif 56 <= age <= 60:
tax = (salary * 0.13)
elif 61 <= age <= 65:
tax = (salary * 0.075)
else:
tax = (salary * 0.05)
return tax
Also, I'm trying to learn so could you explain by putting #comments into the code :) Thank you!
1. Rewrite the code structure
First of all, I think most programmers do like to put function blocks all together and leave the main logic as clean/short as possible to improve readability. Therefore the 'code structure' like this could be hard to maintain or update in the future.
while True: #Loop the whole program
from datetime import datetime, date #Get the Age of from user input
def calculate_age(born): ...
def get_salary(): ...
def contribution(age): ...
if __name__ == "__main__":
# main logic
...
So this kind of structure is really weird, plus you have lots of variables (date_of_birth, age) declared between functions. Would be hard to do the update/maintenance.
If I were you, I'd firstly revise the code like this way
from datetime import datetime, date #Get the Age of from user input
def calculate_age(born): ...
def get_salary(): ...
def contribution(age): ...
if __name__ == "__main__": # It's as if the interpreter inserts this at the top of your module when run as the main program.
program_continue = 'y'
while program_continue.upper() in ['Y', 'YES']:
print("Please enter your date of birth (dd mm yyyy)")
date_of_birth = datetime.strptime(input("--->"), "%d %m %Y")
age = calculate_age(date_of_birth)
print("You are " ,int(age), " years old.")
salary = get_salary() #get salary from get_salary()
tax = contribution(age) #calculate the tax
print("you have to pay", tax, " every month ")
program_continue = str(input("Do you need to do another calculation? (y/n): "))
print("Thank you for using this Calculator, Goodbye")
2. Introduce data structure? or class?
Honestly I don't quite understand what do you mean by "using data structure", so I guess make a "class" is the one you wish. Then you have to consider some points:
what should be the attributes for this class? dob, salary, anything else?
what will you expand in the future? name? gender? contact_info? handicapped or not?
Whatever, we just create a class with dob and salary for now.
class Person(object):
def __init__(self, dob, salary):
"""
input dob and salary only, age and tax will be calculated then
"""
self.dob = dob
self.salary = salary
self.age = self.calculate_age()
self.tax = self.contribution()
def calculate_age(self):
today = date.today()
return today.year - self.dob.year - ((today.month, today.day) < (self.dob.month, self.dob.day))
def contribution(self):
if self.age <= 35:
tax = (self.salary * 0.20)
elif 36 <= self.age <= 50:
tax = (self.salary * 0.20)
elif 51 <= self.age <= 55:
tax = (self.salary * 0.185)
elif 56 <= self.age <= 60:
tax = (self.salary * 0.13)
elif 61 <= self.age <= 65:
tax = (self.salary * 0.075)
else:
tax = (self.salary * 0.05)
return tax
So once you create a variable in the class Person, you can access the age, salary, tax via .age, .salary, .tax.
Please notice that I did not put the function get_salary() into the class since it's a function for "asking user's salary" and has nothing to do with the attribute.
The main logic can be rewritten to:
if __name__ == "__main__": # It's as if the interpreter inserts this at the top of your module when run as the main program.
program_continue = 'y'
while program_continue.upper() in ['Y', 'YES']:
print("Please enter your date of birth (dd mm yyyy)")
date_of_birth = datetime.strptime(input("--->"), "%d %m %Y")
salary = get_salary() #get salary from get_salary()
##### THESE ARE THE CHANGES START
person_obj = Person(date_of_birth, salary)
print("You are ", int(person_obj.age), " years old.")
print("you have to pay", int(person_obj.tax), " every month ")
##### THESE ARE THE CHANGES END
program_continue = str(input("Do you need to do another calculation? (y/n): "))
print("Thank you for using this Calculator, Goodbye")
3. Future expansion
Let's say if I want to add name as an attribute now. All I have to do is to modify the Person class. Should be easier for you to update in the future.
Your function is fairly easy to maintain, especially if you rewrite it a little simpler:
def contribution(age):
if age <= 35:
rate = 0.20
elif age <= 50:
rate = 0.20
elif age <= 55:
rate = 0.185
elif age <= 60:
rate = 0.13
elif age <= 65:
rate = 0.075
else:
rate = 0.05
return salary * rate
But you are right to be worried about embedding data in your code like this. So you may do better with something like this:
# near top or read from file:
tax_rates = [ # upper age limit for each rate
(35, 0.20),
(50, 0.20),
(55, 0.185),
(60, 0.13),
(65, 0.075),
(1000, 0.05)
]
# then elsewhere in the code:
def contribution(salary, age):
for limit, rate in tax_rates:
if age <= limit:
return salary * rate
else:
# finally a good use for for ... else!
raise ValueError(f"Age {age} is out of range.")
I would also recommend moving your import statements and function definitions above the main loop and passing values into the functions (e.g., salary) rather than using global variables. That will make it easier to see what is going on.
Related
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])
https://www.101computing.net/entry-fees-calculator-using-a-flowchart/
I was trying to do the coding challenge my school sent me (link attached above) and I got quite stuck so I was hoping if someone could help. I was stuck when trying to add the discount calculating part because I had to see if the price was more than 50 and if it was, apply a 5% discount to it
def ThemePark():
age = int(input('Enter age: '))
if age <= 15:
print("Your entry price is £11")
if age >= 15:
print("Your entry price is £13.50")
if age >= 18:
print("Your entry price is £15 ")
discount = get_discount(price1 + price2 + price3)
print_discount_message(discount)
price > 50
price1 = 11
price2 = 13.50
price3 = 15
price > 50
discountprice = calculate_discount_price(price, discount)
print(f' Your price: {discd} (original price: {price})')
def Discount(price):
if price > 50:
discount = 0.95
else:
discount = 0.0
return discount
def print_discount_message(discount):
if discount == 0.0:
print(' Not qualified for family discount.')
else:
print(' Qualified for discount: {}%'.format(int(discount * 100)))
def calculate_discount_price(original_price, discount):
return round(original_price - original_price * discount, 2)
if __name__ == '__main__':
while True:
ThemePark()
more = input('Buy more? (Yes/No): ')
if more != 'Yes':
break
def print_discount_message(discount):
if discount == 0.0:
print(' Not qualified for family discount.')
else:
print(' Qualified for discount: {}%'.format(int(discount * 100)))
To output the print(' Qualified for discount: {}%'.format(int(discount * 100)))
you should have:
print(' Qualified for discount: {}%'.format(int((1 - discount) * 100)))
fee = []
age = int(input("Enter age: "))
while age != '':
age = int(input("Enter age: "))
if age <= 5:
fee.append(0)
elif age >= 6 and age <= 64:
fee.append(50.00)
else:
fee.append(25.00)
total = sum(fee)
print("Total payment: ", total)
I want to make a program that would sum up the given entrance fees. I don't even know if I'm doing it correctly
You can't compare a string to an integer, that's your main problem. If you retrieve it from the user as a string and check it is indeed an integer or not will work. That code should do the trick:
def RepresentsInt(s):
try:
int(s)
return True
except ValueError:
return False
fee = []
r='start'
while r != '':
r = input("Enter age: ")
if RepresentsInt(r):
age = int(r)
if age <= 5:
fee.append(0)
elif age >= 6 and age <= 64:
fee.append(50.00)
else:
fee.append(25.00)
total = sum(fee)
print("Total payment: ", total)
fee = 0
age = int(input("Enter age: "))
while age != '':
try:
age = int(input("Enter age: ")) // to avoid exception of string
except:
break
if age <= 5:
fee += 0 // no need to append if outcome is a number
elif age >= 6 and age <= 64:
fee += 50
else:
fee += 25
total = fee
print("Total payment: ", total)
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'm pretty new to coding so bear with me but how do I make this code work? I'm trying to take the information that is entered by the user and input it into my functions so I can print the total.
def hotel_cost(nights):
return nights * 140
def plane_cost(city):
if city == "Atlanta":
return 220
elif city == "Boston":
return 550
elif city == "Chicago":
return 900
def rental_car_cost(days):
if days >= 7:
return days * 100 - 50
elif days >= 1 and days <= 5:
return days * 100 - 20
a = raw_input("Please choose number of nights: ")
b = raw_input("Please choose which city you are flying to (Atlanta, Boston, Chicago) ")
c = raw_input("Please choose days of rental car use: ")
d = raw_input("Please choose how much spending money you plan on spending: ")
a = nights
b = city
c = days
total = a + b + c + d
print "Your total cost of trip is %d" % total
You need to first convert your numeric input values to numbers and then pass your input values to your functions.
Delete the lines
a = nights
b = city
c = days
Calculate your total with
total = hotel_cost(int(a)) + plane_cost(b) + rental_car_cost(int(c)) + float(d)
I assumed that for the number of nights and rental car days only integers make sense.
I am not sure if this applies in Python version 2. However, you could try this:
a = int(raw_input("Please choose number of nights: ") * 140 )
# Multiplies by 140, but this would defeat the purpose of your functions.
And apply the int function to convert all of your inputs into integers.
You can use input() in Python 2 as it evaluates the input to the correct type. For the functions, you just need to pass your values into them, like this:
def hotel_cost(nights):
return nights * 140
def plane_cost(city):
if city == "Atlanta":
return 220
elif city == "Boston":
return 550
elif city == "Chicago":
return 900
def rental_car_cost(days):
if days >= 7:
return days * 100 - 50
elif days >= 1 and days <= 5:
return days * 100 - 20
nights = input("Please choose number of nights: ")
city = raw_input("Please choose which city you are flying to (Atlanta, Boston, Chicago) ")
rental_duration = input("Please choose days of rental car use: ")
spending_money = input("Please choose how much spending money you plan on spending: ")
total = hotel_cost(nights) + plane_cost(city) + rental_car_cost(rental_duration) + spending_money
print "Your total cost of trip is %d" % total