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

Above is my sample code While entering 'no' to my query in while loop its not terminating in first attempt

output i'm getting:

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

iam new to python plz help.

rdas
  • 20,604
  • 6
  • 33
  • 46
Niya
  • 41
  • 3
  • 3
    Your function is recursive. Each time you say "yes", your function goes once more into the recursion stack. Then each time you say "no" your function comes out of recursion once. So you'll need to say "no" for each time you invoked the function. remove the recursion by calling your function in a loop instead. – rdas Apr 03 '21 at 07:11
  • Would be easier for us to look into it if you can at least explain what you are trying to achieve with this code – Suyash Krishna Apr 03 '21 at 07:11
  • As a side note, you should really have a look at [PEP 8](https://www.python.org/dev/peps/pep-0008/) that lists the common, very largely respected conventions of Python code. Just respecting the first rule: "Use 4 spaces per indentation level." would make your code much easier to read. – Thierry Lathuille Apr 03 '21 at 07:20

1 Answers1

0
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()
Kritarth
  • 48
  • 11