How can I make the code stop when the input<0 and when the lowest tax is the same. I have 4 countries and for example if Denmark and Norway have the same tax it will print out: "Denmark Norway" in alphabetical row. Here is an example: Income: 1000000 Lowest tax: 150000.0 USA
Income: 6000 Lowest tax: 1500.0 Denmark Norway USA
Income: -1
Here is my code, it prints out the lowest value and it's country:
`
continue_input = True
income = 0
while continue_input and income >= 0:
income = int(input('Income: '))
# Canada
canada_tax = 0.26
canada = canada_tax * income
# Norway
if income > 3000:
norway_tax1 = 0.1 * 3000
tax_left = income - 3000
norway_tax2 = 0.4 * tax_left
norway_tax = norway_tax1 + norway_tax2
elif income < 3000:
norway_tax = 0.1 * income
# Denmark
denmark_tax = 0
percent = 0
for _ in range(int(income/1000)):
denmark_tax += percent * 1000
percent += 0.1
denmark_tax += percent * (income%1000)
# USA
if income <= 1500:
USA_tax = 0.12 * income
elif income > 1500 and income <= 6000:
USA_tax = 0.25 * income
elif income > 6000 and income <= 10000:
USA_tax = 0.38 * income
elif income > 10000:
USA_tax = 0.15 * income
#Print lowest tax
if canada < norway_tax and canada < USA_tax and canada < denmark_tax:
print(f'Lowest tax: {canada}')
print('Canada')
elif norway_tax < canada and norway_tax < USA_tax and norway_tax < denmark_tax:
print(f'Lowest tax: {norway_tax}')
print('Norway')
elif USA_tax < canada and USA_tax < norway_tax and USA_tax < denmark_tax:
print(f'Lowest tax: {USA_tax}')
print('USA')
elif denmark_tax < canada and denmark_tax < norway_tax and denmark_tax < USA_tax:
print(f'Lowest tax: {denmark_tax}')
print('Denmark')
I'm expecting this:
Income: 1000000
Lowest tax: 150000.0
USA
Income: 6000
Lowest tax: 1500.0
Denmark Norway USA
Income: -1
Since, you are taking the input inside the while loop, It will first execute all the calculations, then print and then check if income is less than 0 or not.
To avoid this you can remove income>=0
condition from the while loop and instantly check if the value is negative or not. Also your code will not print if more than 1 country has minimum tax because you are only checking for less value and not equal.
A simple and easy way of solving both the above problem can be:-
continue_input = True
income = 0
# changed here
while continue_input :
income = int(input('Income: '))
# changed here
if income < 0 :
break
# Canada
canada_tax = 0.26
canada = canada_tax * income
# Norway
if income > 3000:
norway_tax1 = 0.1 * 3000
tax_left = income - 3000
norway_tax2 = 0.4 * tax_left
norway_tax = norway_tax1 + norway_tax2
elif income < 3000:
norway_tax = 0.1 * income
# Denmark
denmark_tax = 0
percent = 0
for _ in range(int(income/1000)):
denmark_tax += percent * 1000
percent += 0.1
denmark_tax += percent * (income%1000)
# USA
if income <= 1500:
USA_tax = 0.12 * income
elif income > 1500 and income <= 6000:
USA_tax = 0.25 * income
elif income > 6000 and income <= 10000:
USA_tax = 0.38 * income
elif income > 10000:
USA_tax = 0.15 * income
# changed here
min_tax = min(canada, norway_tax, USA_tax, denmark_tax)
print(f'Lowest tax: {min_tax}', end = " ")
if min_tax == canada :
print('Canada', end = " ")
if min_tax == denmark_tax :
print('Denmark', end = " ")
if min_tax == norway_tax :
print('Norway', end = " ")
if min_tax == USA_tax :
print('USA',end= " ")
print()
output:-
Income: 1000000 Lowest tax: 150000.0 USA Income: 6000 Lowest tax: 1500.0 Denmark Norway USA Income: -1
PS:- I tweaked the print statements using end = " "
so that you can get the output in desired form. I have also commented before I have made changes in the code
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.