[英]How can I make the code stop when input < 0 and when lowest tax is the same then it prints out the following countries
How can I make the code stop when the input<0 and when the lowest tax is the same.当输入 <0 且最低税相同时,如何使代码停止。 I have 4 countries and for example if Denmark and Norway have the same tax it will print out: "Denmark Norway" in alphabetical row.
我有 4 个国家,例如,如果丹麦和挪威有相同的税收,它将打印出:按字母顺序排列的“丹麦挪威”。 Here is an example: Income: 1000000 Lowest tax: 150000.0 USA
这是一个例子: 收入:1000000 最低税收:150000.0 美国
Income: 6000 Lowest tax: 1500.0 Denmark Norway USA收入:6000 最低税:1500.0 丹麦 挪威 美国
Income: -1收入:-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收入:100万
Lowest tax: 150000.0最低税:150000.0
USA美国
Income: 6000收入:6000
Lowest tax: 1500.0最低税:1500.0
Denmark Norway USA丹麦 挪威 美国
Income: -1收入:-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.因为,你在 while 循环中获取输入,它会首先执行所有计算,然后打印,然后检查收入是否小于 0。
To avoid this you can remove income>=0
condition from the while loop and instantly check if the value is negative or not.为避免这种情况,您可以从 while 循环中删除
income>=0
条件并立即检查该值是否为负。 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.如果超过 1 个国家/地区有最低税,您的代码也不会打印,因为您只检查较少的价值而不是相等的。
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:- 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. PS:- 我使用
end = " "
调整了打印语句,以便您可以获得所需形式的 output。 I have also commented before I have made changes in the code我在更改代码之前也发表了评论
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.