简体   繁体   中英

How do I infinitely loop this program in python?

Alright, I'm just starting to learn python, and have no idea where else to pose this question. I wrote this calculator and it works.

a,b=input("Enter two numbers (sperated by a comma): ")
operator=raw_input("Do you want to add, subtract, multiply, or divide? ")
if operator=="divide":
    print "Your answer is", a / b
if operator=="multiply":
    print "Your answer is", a * b
if operator=="add":
    print "Your answer is", a + b
if operator=="subtract":
    print "Your answer is", a - b    
repeat=raw_input("Do you want to perform another calculation(yes or no)? ")

Now, I want to know how to start this program all over again if the input is yes. I searched for an answer, but I couldn't figure out the command for looping the entire thing. Like in while loops, you have to give a command after the condition but what is that command? ples halp.

Wrap it in a while loop:

repeat = "yes"
while repeat.lower().strip() == 'yes':
    a,b=input("Enter two numbers (sperated by a comma): ")
    operator=raw_input("Do you want to add, subtract, multiply, or divide? ")
    if operator=="divide":
        print "Your answer is", a / b
    if operator=="multiply":
        print "Your answer is", a * b
    if operator=="add":
        print "Your answer is", a + b
    if operator=="subtract":
        print "Your answer is", a - b    
    repeat=raw_input("Do you want to perform another calculation(yes or no)? ")

This will loop as long as the answer is 'yes' (case-insensitive). The initial 'repeat' is to guarantee the loop runs at least once.

By the way, you can turn your if test into a dict:

import operator
ops = {'divide': operator.div,
       'multiply': operator.mul,
       'add': operator.add,
       'subtract': operator.sub}
repeat = "yes"
while repeat.lower().strip() == 'yes':
    a,b=input("Enter two numbers (sperated by a comma): ")
    operator=raw_input("Do you want to add, subtract, multiply, or divide? ").lower().strip()
    if operator not in ops:
        print 'Invalid operator:', operator
        continue
    print "Your answer is", ops[operator](a,b)
    repeat=raw_input("Do you want to perform another calculation(yes or no)? ")
while True:
    a,b=input("Enter two numbers (sperated by a comma): ")
    operator=raw_input("Do you want to add, subtract, multiply, or divide? ")
    if operator=="divide":
        print "Your answer is", a / b
    if operator=="multiply":
        print "Your answer is", a * b
    if operator=="add":
        print "Your answer is", a + b
    if operator=="subtract":
        print "Your answer is", a - b    
    repeat=raw_input("Do you want to perform another calculation(yes or no)? ")
    if repeat == 'no':
        break

While True continues looping forever, it can be broken by the keyword break (It breaks out of the nearest loop(for or while)).

When processing input from the user to determine a condition, it's preferable to check if the input from the user starts with "y" (yes) or starts with "n" (no), since the user might input y instead of yes and he might mess with the capitalization, here's a code that implements this:

while True:
    a,b=input("Enter two numbers (sperated by a comma): ")
    operator=raw_input("Do you want to add, subtract, multiply, or divide? ")
    if operator=="divide":
        print "Your answer is", a / b
    if operator=="multiply":
        print "Your answer is", a * b
    if operator=="add":
        print "Your answer is", a + b
    if operator=="subtract":
        print "Your answer is", a - b    
    repeat=raw_input("Do you want to perform another calculation(yes or no)? ")
    if repeat.lower.startswith('n'):
        break 

"repeat" is a string, it has a method(Similiar to a function) called "lower", this method returns the lowercase representation of the string, then you can check if that lowercase representation(Also a string) starts with the letter "n" by using another method called startswith.

Try this
You can use while True: with this the code written inside the for loop
will execute infinitely,
To stop execution you can use Ctrl + C
while True:
    a,b=input("Enter two numbers (sperated by a comma): ")
    operator=raw_input("Do you want to add, subtract, multiply, or divide? ")
    if operator=="divide":
        print "Your answer is", a / b
    if operator=="multiply":
        print "Your answer is", a * b
    if operator=="add":
        print "Your answer is", a + b
    if operator=="subtract":
        print "Your answer is", a - b
    repeat=raw_input("Do you want to perform another calculation(yes or no)? ")     if repeat == "no":         break

Try this:

while True:    
    a,b=input("Enter two numbers (sperated by a comma): ")
    operator=raw_input("Do you want to add, subtract, multiply, or divide? ")
    if operator=="divide":
        print "Your answer is", a / b
    if operator=="multiply":
        print "Your answer is", a * b
    if operator=="add":
        print "Your answer is", a + b
    if operator=="subtract":
        print "Your answer is", a - b    
    repeat=raw_input("Do you want to perform another calculation(yes or no)? ")
    if repeat == "no":
        break
for x in repeat.upper():
   a,b=input("Enter two numbers (sperated by a comma): ")
   operator=raw_input("Do you want to add, subtract, multiply, or 
   divide? ")
   if operator=="divide":
      print "Your answer is", a / b
   if operator=="multiply":
      print "Your answer is", a * b
   if operator=="add":
      print "Your answer is", a + b
   if operator=="subtract":
      print "Your answer is", a - b    
   if oper == repeat:
      break

it will loop untill the certain condition true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM