python 打印最小的数字,不使用列表、元组和字典

bf1o4zei  于 2023-01-29  发布在  Python
关注(0)|答案(2)|浏览(105)

我可以使用for循环、while和if语句。我一直在纠结如何才能做到这一点。我试着使用这个,但is有一个元组。Canada_tax、Norway_tax、USA_tax和Denmark_tax有一个值,计算都在这个代码上面。

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()

我想要发生的是:
收入:100万
最低税额:150000.0
美国
收入:6000
最低税额:1500.0
丹麦挪威
收入:-1
当国家有相同的最低税率,它应该打印出来的字母行。

s2j5cfk0

s2j5cfk01#

Python有一个名为min的原生函数,它可以帮你完成计算,但是如果你需要手动完成,你可以从一种税(比如加拿大的税)开始,然后用其他更小的值替换变量:
(by顺便说一下,您不应该将变量命名为min

minTax = canada
if minTax > norway_tax:
    minTax = norway_tax
if minTax > USA_tax:
    minTax = USA_tax
if minTax > denmark_tax:
    minTax = denmark_tax
    

if minTax == canada :
   print('Canada')

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

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

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

使用min函数可以避免第一组if:

minTax = min(canada, norway_tax, USA_tax, denmark_tax)
bogh5gae

bogh5gae2#

我以前用过这个,但觉得太乱了:

#   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')

它是有效的,但如果各国都有相同的最低税率,我能做些什么来使它有效,应该是这样的:

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

下面是我的全部代码:

continue_input = True
income = 0

while continue_input and income >= 0:
    income = int(input('Income: '))

    if income >= 0:
    

    # 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')
     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')

# Print lowest if countries have the same lowest tax in alphabetic row.

相关问题