Home > Software engineering >  Print what is the lowest number without lists, tuples and dictionaries
Print what is the lowest number without lists, tuples and dictionaries

Time:01-27

I can use for loops, while and if statements. I'm struggling on how I can do that. I tried using this but is has a tuple. Canada_tax, Norway_tax, USA_tax and Denmark_tax have a value and the calculations are above this code.

min_tax = (canada, norway_tax, USA_tax, denmark_tax)
     min = min_tax[0]
     for i in min_tax:
        if i < min:
            min = i
     print(f'Lowest tax: {min}')
     
     if min == canada :
        print('Canada')

     if min == denmark_tax :
        print('Denmark')

     if min == norway_tax :
        print('Norway')

     if min == USA_tax :
        print('USA')
     print()

What I want to happen is this:

Income: 1000000
Lowest tax: 150000.0
USA
Income: 6000
Lowest tax: 1500.0
Denmark Norway USA
Income: -1

When the countries have the same lowest tax it should print them out in alphabetic row.

CodePudding user response:

I used this before but thought is was a to much mess:

#   Print lowest tax
     if canada <= norway_tax and canada <= USA_tax and canada <= denmark_tax:
        print(f'Lowest tax: {canada}')
        print('Canada')
     if norway_tax <= canada and norway_tax <= USA_tax and norway_tax <= denmark_tax:
        print(f'Lowest tax: {norway_tax}')
        print('Norway')
     if USA_tax <= canada and USA_tax <= norway_tax and USA_tax <= denmark_tax:
        print(f'Lowest tax: {USA_tax}')
        print('USA')
     if denmark_tax <= canada and denmark_tax <= norway_tax and denmark_tax <= USA_tax:
        print(f'Lowest tax: {denmark_tax}')
        print('Denmark')

It works but not if countries have the same lowest tax, what could I do to make that work, should be like this:

Income: 1000000
Lowest tax: 150000.0
USA
Income: 6000
Lowest tax: 1500.0
Denmark Norway USA
Income: -1

CodePudding user response:

You have a tuple of "tax rates". Create a matching tuple of country names. The reference in your question to "income" makes no sense in context. This may get you part of the way there. Use zip() to enumerate the two tuples in parallel

min_tax = (canada, norway_tax, USA_tax, denmark_tax)
countries = ('Canada', 'Norway', 'USA', 'Denmark')
lo = float('inf')

for t, c in zip(min_tax, countries):
    if t < lo:
        lo = t
        country = c

print(country)
  • Related