Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 One more python question!
Index -> Programming, Python -> Python Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Nathan4102




PostPosted: Tue Mar 26, 2013 7:44 pm   Post subject: One more python question!

Ok, last question of the night, I promise Wink

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
Sponsor
sponsor
Panphobia




PostPosted: 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 Smile

something along the lines of

Python:
if operator == "Q":
        return 1

and
Python:
looping = choices()
nullptr




PostPosted: 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




PostPosted: 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




PostPosted: 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 Smile
Display posts from previous:   
   Index -> Programming, Python -> Python Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: