而循环未在第一次尝试中终止

2024-10-02 14:16:20 发布

您现在位置:Python中文网/ 问答频道 /正文

d = {}
temp = 10000
total = 0
def Expanse_calculator():
  global temp
  buy = True
  while buy == True:
    x = input("Enter the expenditure name:")
    if x in d.keys():
      price =float(input(f" Enter the '{x}' amount:"))
      if temp>=price:
        d[x]+=price
        temp-=price
      else:
        print("insufficint found")
        break
    else:
      price =float(input(f"Enter the '{x}'amount:"))
      if temp>=price:
        d[x]=price
        temp-=price
      else:
        print("insufficint found")
        break
    total=sum(d.values())
    
    while True:
      ip = input("If you want to add any more expenditure [YES|NO]:").lower()
      if ip == 'no':
        buy = False
        break
      elif ip == 'yes':
        Expanse_calculator()
      else:
        print("Invalid Entry")
Expanse_calculator()
print(total)

上面是我的示例代码 在While循环中为我的查询输入“no”时,它不会在第一次尝试时终止

我得到的输出:

Enter the expenditure name:asd
Enter the 'asd'amount:123
If you want to add any more expenditure [YES|NO]:yes
Enter the expenditure name:asf
Enter the 'asf'amount:124
If you want to add any more expenditure [YES|NO]:no
If you want to add any more expenditure [YES|NO]:no

我不熟悉python plz帮助


Tags: thetoyouaddinputifamountprice
1条回答
网友
1楼 · 发布于 2024-10-02 14:16:20
d = {}
temp = 10000
total = 0
def Expanse_calculator():
  global temp,total
  buy = True
  while buy == True:
    x = input("Enter the expenditure name: ")
    if x in d.keys():
      price =float(input(f" Enter the '{x}' amount: "))
      if temp>=price:
        d[x]+=price
        temp-=price
      else:
        print("insufficient funds")
        break
    else:
      price =float(input(f"Enter the '{x}'amount: "))
      if temp>=price:
        d[x]=price
        temp-=price
      else:
        print("insufficient funds")
        break
    total=sum(d.values())
    
    while True:
        ip = input("If you want to add any more expenditure [YES|NO]:").lower()
        if ip == 'no':
            buy = False
            break
        elif ip == 'yes':
            Expanse_calculator()
        else:
            print("Invalid Entry")
    print(total)
    quit()
Expanse_calculator()

相关问题 更多 >

    热门问题