One more python question!
Author |
Message |
Nathan4102
|
Posted: Tue Mar 26, 2013 7:44 pm Post subject: One more python question! |
|
|
Ok, last question of the night, I promise
So I've got the code below, running it in Wing IDE (Did the same thing in other IDE's) and for some reason, when I type Q, it doesn't quit my while loop! I define looping once at the top, and never set it to 0 again, yet my debugging print commands show that looping is being set to 0 again! Is there something about Python that I'm missing, or have I screwed up somewhere?
Thaaanks!
Nathan
Example Output:
code: | 0
Choices:
Addition - A
Subtract - S
Multiply - M
Divide - D
Quit - Q
The operator I'd like to use is: Q
Quit!
1
0
Choices:
Addition - A
Subtract - S
Multiply - M
Divide - D
Quit - Q
The operator I'd like to use is: |
Program:
Python: | looping = 0
def choices():
print "Choices:"
print "Addition - A"
print "Subtract - S"
print "Multiply - M"
print "Divide - D"
print "Quit - Q"
operator = raw_input("The operator I'd like to use is: ")
if operator == "A":
num1 = raw_input("The first number I'd like to add is: ")
num2 = raw_input("The second number I'd like to add is: ")
print("The answer is",add(num1, num2))
print ""
if operator == "S":
num1 = raw_input("The first number I'd like to subtract is: ")
num2 = raw_input("The second number I'd like to subtract is: ")
print("The answer is",subtract(num1, num2))
print ""
if operator == "M":
num1 = raw_input("The first number I'd like to multiply is: ")
num2 = raw_input("The second number I'd like to multiply is: ")
print("The answer is",multiply(num1, num2))
print ""
if operator == "D":
num1 = raw_input("The first number I'd like to divide is: ")
num2 = raw_input("The second number I'd like to divide is: ")
print("The answer is",divide(num1, num2))
print ""
if operator == "Q":
looping = 1
print "Quit!" #DEBUG
print looping #DEBUG
print "" #DEBUG
def add(num1, num2):
return float(num1) + float(num2)
def subtract(num1, num2):
return float(num1) - float(num2)
def multiply(num1, num2):
return float(num1) * float(num2)
def divide(num1, num2):
return float(num1) / float(num2)
while looping == 0:
print looping
choices() |
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
Panphobia
|
Posted: Tue Mar 26, 2013 9:03 pm Post subject: Re: One more python question! |
|
|
Try returning a value from the function choices and storing it in looping, that should work
something along the lines of
Python: | if operator == "Q":
return 1 |
and
Python: | looping = choices() |
|
|
|
|
|
|
nullptr
|
Posted: Tue Mar 26, 2013 10:02 pm Post subject: Re: One more python question! |
|
|
Panphobia's suggestion is good, but if you want to keep your code structured the same way, add "global looping" to the start of your choices() function. You need this line because Python thinks you want to define a new variable local to your function -- it doesn't consider that you've already defined a variable called looping. Adding this line tells Python that you're talking about the global variable and not a local one. |
|
|
|
|
|
Nathan4102
|
Posted: Wed Mar 27, 2013 7:43 am Post subject: RE:One more python question! |
|
|
Thanks for explaining it Null, it makes sense now! |
|
|
|
|
|
Cancer Sol
|
Posted: Wed Mar 27, 2013 6:03 pm Post subject: Re: One more python question! |
|
|
Just post more questions if you need any, I'm sure people will still gladly help you |
|
|
|
|
|
|
|