【问题标题】:There is a way to use less than this number of "if" There is a way to shorten this code有一种方法可以使用少于这个数量的“如果”有一种方法可以缩短这个代码
【发布时间】:2023-03-03 15:02:01
【问题描述】:
h = int(input ("Enter your working hours in a week:"))
rate = 8
if ((h < 0 ) or (h > 168)):
print("INVALID")
elif h <= 40:
print ("YOU MADE", rate*h, "DOLLARS THIS WEEK")
elif 41 <= h <= 50:
print("YOU MADE", int(40 * rate + (h - 40) * (1.129 * rate)), "DOLLARS THIS WEEK")
else:

print("YOU MADE",int(40 * rate + (h - 40) * 1.20373 * rate), "DOLLARS THIS WEEK")

【问题讨论】:

  • (h &lt; 0 ) and (h &gt; 168) 是不可能的 - 你的意思是 or 吗?
  • 你说得对,我写的很匆忙,我的意思是一样的或

标签: python python-3.x printing format


【解决方案1】:

首先(简单的解决方案):只需将第 3 行中的 'and' 运算符替换为 'or' 运算符即可轻松解决问题。

您还可以根据您的情况强制用户继续输入正确的数字,并使用以下代码添加额外的安全性检查输入的数字是否为 int-

good_Input = False
while not good_Input:
    try:
        number = int(input('Enter a number: '))
        if number > 0 and number < 168:
            good_Input = True
            print("that's a good number. Well done!")
        else:
            print("that's not a good number number. Try again: ")
    except ValueError:
        print("that's not an integer. Try again: ")

只需在第一行添加这个 sn-p 和 BINGOO..!!

【讨论】:

    【解决方案2】:

    if ((h 168)): ---------> 这是错误的条件在这里使用'or'而不是'and'

    如果有帮助,请检查此代码:

    h = int(input ("Enter your working hours in a week:"))
    rate = 8
    
    print((lambda: "Value is valid", lambda: "INVALID")[h < 0 or h > 168]())
    print((lambda: "Hours clocked is not in range of 1 to 40 ", lambda: ("YOU MADE", rate*h, "DOLLARS THIS WEEK"))[1 < h <= 40]())
    print((lambda: "Hours clocked is not in range of 41 to 50 ", lambda: ("YOU MADE", int(40 * rate + (h - 40) * (1.129 * rate)), "DOLLARS THIS WEEK"))[41 <= h <= 50]())
    

    【讨论】:

    • 打印十进制结果我只想把整数打印在输出上
    • 我修好了,伙计,请看我的代码,您只需输入 int(output) 即可将您的十进制输出转换为 int 。
    • 在你的代码中输入数字 58 然后运行它,然后在我的代码中输入 58 然后运行它,检查差异
    • 您的代码将获得:输入您一周的工作时间:58 您本周赚了 493.33712 美元 =================
    • 我的输出将是:输入你一周的工作时间:58 你这周赚了 493 美元
    【解决方案3】:

    您可以使用 max 和 min 内置函数来避免使用 ifs 来确定有多少小时属于特定范围。

    符合加班条件的小时数是(hours - 50)0 中的较大者。 符合标准工资标准的小时数是hours40 中的较小者。

    要确定适用的加班费率,我们可以将这两个费率放在一个元组中,首先是较低的费率。我们使用hours &gt;= 50 的结果从元组中获取所需速率的索引。这是因为 hours &gt;= 50 计算为布尔值 - TrueFalse - 并且布尔值是 int 的子类,因此如果 hours &gt;= 50False,则索引为 0 并选择较低的速率,否则选择较高的比率。

    rate = 8 
    more = True
    while more:
        h = int(input("Enter your working hours in a week:"))
        if 0 < h < 168:
            overtime_rate = (1.129, 1.20373)[h >= 50] * rate
            overtime = max(h - 40, 0) * overtime_rate
            standard_time = min(40, h) * rate
            total_pay = int(standard_time + overtime)
            print(f'YOU MADE {total_pay} DOLLARS THIS WEEK')
        else:
            print('INVALID')
        again = input('More (y/n)?').lower()
        more = (again == 'y' or again == 'yes')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-19
      • 1970-01-01
      相关资源
      最近更新 更多