Reputation: 1
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.
Upvotes: 0
Views: 7164
Reputation: 1
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
Upvotes: 0
Reputation: 10298
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)? ")
Upvotes: 3
Reputation: 131
Try this
You can usewhile 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
Upvotes: 0
Reputation: 2320
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
Upvotes: 0
Reputation: 742
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.
Upvotes: 2