Question:
How can I use While Loops in Python?
zedChigo
2014-06-11 03:35:34 UTC
Well basically I've more or less just started with Python.

I just made a calculator with if/else/elif statements,
but because I don't have a loop the program stops after I've done the equasion I need

How will I add a while loop statement to my code?

Here is my code:

print "1: Addition"
print "2: Subtraction"
print "3: Multiplication"
print "4: Dividing"
print "\n"

choice = int(raw_input("Which do you want? 1/2/3/4 \n Choice: "))

if choice == 1:
print "Now this is Addition"
a = raw_input("Choose your first number: ")
b = raw_input("Choose your second number: ")
c = float(a) + float(b)
print c

elif choice == 2:
print "This is Subtraction"
a = raw_input("Choose your first number: ")
b = raw_input("Choose your second number: ")
c = float(a) - float(b)
print c

elif choice == 3:
print "This is Multiplication"
a = raw_input("Choose your first number: ")
b = raw_input("Choose your second number: ")
c = float(a) * float(b)
print c

elif choice == 4:
print "This is Dividing"
a = raw_input("Choose your first number: ")
b = raw_input("Choose your second number: ")
c = float(a) / float(b)
print c


else:
print "This is wrong, try again"
Three answers:
?
2014-06-11 09:36:14 UTC
Indent eveything aswell

Loop = 0

while (Loop == 0):

print "1: Addition"

print "2: Subtraction"

print "3: Multiplication"

print "4: Dividing"

print "\n"



choice = int(raw_input("Which do you want? 1/2/3/4 \n Choice: "))



if choice == 1:

print "Now this is Addition"

a = raw_input("Choose your first number: ")

b = raw_input("Choose your second number: ")

c = float(a) + float(b)

print c



elif choice == 2:

print "This is Subtraction"

a = raw_input("Choose your first number: ")

b = raw_input("Choose your second number: ")

c = float(a) - float(b)

print c



elif choice == 3:

print "This is Multiplication"

a = raw_input("Choose your first number: ")

b = raw_input("Choose your second number: ")

c = float(a) * float(b)

print c



elif choice == 4:

print "This is Dividing"

a = raw_input("Choose your first number: ")

b = raw_input("Choose your second number: ")

c = float(a) / float(b)

print c





else:

print "This is wrong, try again"
Morgan
2014-06-11 04:20:52 UTC
where do you want the while loop?

you need a bollean variable that cahanges so A= True



while A= True:

blah blah



when you want the loop to staop simply change A to False



A=False



make sure a is defined above where it changes to False though
anonymous
2014-06-11 03:43:25 UTC
What that guy said


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...