[英]Can't figure out how to print the following outputs
I'm printing out lowest tax from my project, but I'm stuck on this problem.我正在从我的项目中打印出最低税,但我被这个问题困住了。 How can I change this output:
如何更改此 output:
Income: 6100
Lowest tax: 1540.0
1540.0
Norway
Income: 9000
Lowest tax: 2340.0
2340.0
Canada
To this:对此:
Income: 1000000
Lowest tax: 150000.0
USA
Income: 6000
Lowest tax: 1500.0
Denmark Norway USA
Income: -1
Here is my code:这是我的代码:
continue_input = True
income = 0
while continue_input :
income = int(input('Income: '))
if income < 0 :
continue_input = not True
# 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
min_tax = min(canada, norway_tax, USA_tax, denmark_tax)
print(f'Lowest tax: {min_tax}')
print(min_tax)
if min_tax == canada :
print('Canada')
if min_tax == denmark_tax :
print('Denmark')
if min_tax == norway_tax :
print('Norway')
if min_tax == USA_tax :
print('USA')
print()
You want to break out of the while loop immediately if income is < 0, so:如果收入 < 0,您想立即跳出 while 循环,因此:
if income < 0: break如果收入 < 0:中断
Delete the line print(min_tax)
, it's printing min_tax an extra time.删除行
print(min_tax)
,它会额外打印 min_tax 。
To put the country names all on the same line, do the following:要将国家名称全部放在同一行,请执行以下操作:
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()
Adding end=" "
to print()
will prevent it from adding a newline.将
end=" "
添加到print()
将阻止它添加换行符。 It will add a space instead.它将添加一个空格。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.