“有人可以帮我找到Python中的最小开支吗?”

huangapple go评论75阅读模式
英文:

can someone help me to find minimum expenditure in python

问题

我已经翻译了您提供的Python代码,以下是翻译的部分:

exp =-1
total=0
maxexp=0
minexp=`0

while exp!=0:

    exp = int(input("输入您的支出金额: "))
    total= total + exp

    if exp < minexp:
        minexp = exp

print("总支出金额为: " + str(total))
print("您的最小支出金额为: " + str(minexp))

请注意,我已经将代码中的HTML实体(如&quot;)替换为普通的双引号和大于号。此外,我还更正了代码中的拼写错误,将"maxexp"和"minexp"初始化为0,以确保代码按预期工作。

英文:

I have written the below Python code to find the total and minimum expenditures. However, when I execute this code, the minimum expenditure only returns zero, while the maximum and total expenditures are working perfectly.

exp =-1
total=0
maxexp=0
minexp=`0

while exp!=0:

    exp = int(input(&quot;enter your expenditure: &quot;))
    total= total + exp

    if exp &lt; minexp:
        minexp = exp

print(&quot;`Total `expenditure is: &quot; + str(total))
print(&quot;your minimum expenditure is: &quot; + str(minexp))

“有人可以帮我找到Python中的最小开支吗?”

答案1

得分: 1

为了跟踪最小金额,您应该将minexp初始化为一个人工设置的高数值:

total = 0
minexp = float('inf')

while (exp := int(input('输入您的支出(输入0退出):'))) != 0:
    total += exp
    minexp = min(exp, minexp)

if total:
    print(f'总支出为 {total}')
    print(f'最小支出为 {minexp}')
英文:

In order to keep track of the minimum amount you should initialise minexp to an artificially high number:

total = 0
minexp = float(&#39;inf&#39;)

while (exp := int(input(&#39;enter your expenditure (0 to quit): &#39;))) != 0:
    total += exp
    minexp = min(exp, minexp)

if total:
    print(f&#39;Total expenditure is {total}&#39;)
    print(f&#39;minimum expenditure is {minexp}&#39;)

huangapple
  • 本文由 发表于 2023年5月7日 00:46:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76190052.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定