In this problem it says that the first 10,000 drachs are not taxed, the next 20,000 are taxed 10%, the next 40,000 are taxed 20%, and any after the first 70,000 are taxed 30%. I'm fairly new to python, but this is what i have so far. I'm not sure where i've gone wrong. I think it is because i haven't defined the "tax" variable, but i'm not sure. any help would be much appreciated. THANKS!
**The code must also be terminated if the user enters in a negative number and i'm not sure how to add this to my for loops.
def income(drach):
for drach in range(10000):
tax = 0
for drach in range(10000 , 30000):
tax = ((drach - 10000)*0.1)
for drach in range(30000 , 70000):
tax = ((drach - 30000)*0.2) + 2000
for drach in range(70000 , 10**999):
tax = ((drach - 70000)*0.3) + 10000
print tax
Since they didn't have coined money, ancient households had to pay taxes in kind, and they paid different taxes throughout the year. Poll taxes required each man to deliver a cow or sheep to the authorities. Merchants transporting goods from one region to another were subject to tolls, duty fees, and other taxes.
The earliest known tax was implemented in Mesopotamia over 4500 years ago, where people paid taxes throughout the year in the form of livestock (the preferred currency at the time). The ancient world also had estate taxes and taxes.
Taxes were paid in the shape of gold-coins, cattle, grains, raw-materials and also by rendering personal service. "Most of the taxes of Ancient India were highly productive. The admixture of direct taxes with indirect Taxes secured elasticity in the tax system, although more emphasis was laid on direct tax.
It derives from the Latin taxare which means 'to assess'. Before that, English used the related word 'task', derived from Old French. For a while, 'task' and 'tax' were both in common use, the first requiring labour, the second money. 'Tax' then developed its meaning to imply something wearisome or challenging.
I think this is the correct taxation schema:
def tax(income):
tax=0;
if income > 70000 :
tax += (income-70000)* 0.3
income = 70000
if income > 30000 :
tax += (income-30000)* 0.2
income = 30000
if income > 10000 :
tax += (income-10000)* 0.1
return tax;
The keyword for
is used for looping over iterables. For example
for drach in range(10000):
tax = 0
assigns every value in the range 0, 1, 2, ..., 9998, 9999 to drach
, and executes tax = 0
for each of these. This almost certainly isn't what you want -- you probably want if
instead of for
.
You could use the max()
function to avoid using if
as well:
tax = max(drach - 10000, 0) * 0.1 +
max(drach - 30000, 0) * 0.1 +
max(drach - 70000, 0) * 0.1
You most likely don't want to use for in
construct. In a for x in iterable
construct, you loop through iterable
assigning x
to the next element yielded by iterable
, until you reach the end (i.e. a StopIteration is raised).
Instead, you probably want to preserve the parameter drach
and apply it to the taxation conditions.
As Sven Marnach pointed out:
The check drach in range(10000 , 30000) is very inefficient in Python 2. Better use 10000 <= drach < 30000.
So:
def income(drach):
tax = 0
if 10000 <= drach <= 30000:
tax += ((drach - 10000)*0.1)
if 30000 <= drach <= 70000:
tax += ((drach - 30000)*0.2) + 2000
if drach > 70000:
tax += ((drach - 70000)*0.3) + 10000
return tax
As an alternative you could loop through the slices:
def income(drach):
tax = 0
percent = step = 0.1
lower = 10000
for upper in [30000, 70000]:
if lower < drach <= upper:
tax += (drach - lower) * percent
lower = upper
percentage += step
if lower < drach:
tax += (drach - lower) * percent
return tax
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With