Question

What if i want to ask the user whether he wants to perform another calculation or not everytime the user makes a calculation and gets the answer? I just want the code to run again from start everytime user performs calculation

var = int(raw_input("Enter 1,2,3 or 4 for add,subtract,multiplication,division           respectively: "))
if var == 1:
print "You chose to add.Lets add!! :)"
def main ():
 total = 0.0
 while True:
    number = float(raw_input('enter a number: '))
    total+=number
    if number == 0:
        break
    print "your answer is:",total  
main()
elif var == 3:
print "You chose to multiply.Lets multiply!! :) "    
def main ():
  total = 1.0
  while True:
    number = float(raw_input('enter a number:'))
    total*=number
    if number == 1:
       break
    print "the answer is", total
main()
Was it helpful?

Solution

Just put

while True:

around the whole thing. This will continue looping indefinitely. If you want the user to be able to choose to end the program, try:

while True:
    ...
    continue = raw_input("Continue? (Y/N) ")
    if continue.lower in ("n", "no"):
        break 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top