简体   繁体   English

当输入 < 0 并且最低税率相同时,如何使代码停止,然后打印出以下国家/地区

[英]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.

相关问题 如何使用用户输入的两个连续的相同字符串停止while循环,然后打印出其余的字符串 - how to stop a while loop with two consecutive same strings form user input and then prints out the rest of the strings 当[1,2]出现时如何停止。 这是“ break”的错误吗? - How can I stop when [1, 2] comes out. Is it an error of 'break'? 使用python Tkinter时,如何在显示相同输入文本的同一个类中停止两个输入框? - When using python Tkinter, how can I stop two entry box's in the same class showing the same input text? 如何使以下代码更快? - How can I make the following code faster? 我有这段代码,我想让它在用户输入 q 并打印我想要它做的事情时停止。 接下来我该做什么? - I have this code and I want to make it stop when a user enters q and prints what I want it to do. What do I do next? 我如何定义一个函数并输入两个正整数,这样当它输入负整数或字符串时,它会引发 ValueError 但打印出“无效”? - How do I def a function and input two positive ints such that when it inputs negative ints or strings it raises a ValueError but prints out 'invalid'? 输入错误时,如何再次请求相同的输入? - How can I ask for the same input again when there was incorrect input? 我怎样才能让你输入“工人代码”并获得与“打印(Worker_36.details)”时相同的工人详细信息? - Python - How can I make it so you input a "Worker code" and get the worker details same as when you "print(Worker_36.details)"? - Python 当错误最低时如何执行此代码? - How to execute this code when error is lowest? 如何获取此代码中的最低值? - How can I take the lowest value in this code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM