Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 7th, 2013, 05:42 PM
iam1133 iam1133 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 2 iam1133 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 30 m 49 sec
Reputation Power: 0
Help with defined function and while loop

Here is my program so far in Python 2.7:

def menu():
....print "Welcome to my bitchin' calculator!"
....print " "
....return raw_input("Would you like to add, subtract, multiply, or divide? ")
def add(add1,add2):
....print add1, "+", add2, "=", add1 + add2
def sub(sub1,sub2):
....print sub1, "-", sub2, "=", sub1 - sub2
def redo(a):
....if a == "yes":
........print "OK!"
....else:
........loop = 0
loop = 1
choice = 0
while loop == 1:
....choice = menu()
....if choice == "add":
........add(input("add this: "),input("to this: "))
........redo(raw_input("Would you like to start over? "))
print "Bye bye!"

*Note that the .... was added to indicate indention, due to this forum removing my indentions. in the actual program, the indentions are present

I can choose to add, and then it successfully adds the numbers and starts the redo function as defined above. However when I type "no", it should set loop to 0 and stop running the while loop, but it doesn't! after I type no, it runs menu again and starts over. It never prints "Bye bye!" Can someone help a rookie out?

Reply With Quote
  #2  
Old January 7th, 2013, 07:22 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,458 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 4 Days 6 h 26 m 43 sec
Reputation Power: 403
Code:
def redo(a):
    if a == "yes":
        print "OK!"
    else:
        loop = 0    # loop is local to function redo

loop = 1     # this loop has global scope
choice = 0

Please follow the link at my signature to learn about [c o d e] tags. Thank you for the dots! Appreciated.

The easiest solution is
Code:
def redo(a):
    global loop
    if a == "yes":
        print "OK!"
    else:
        loop = 0    # loop is global

A better solution, because redo doesn't need to know details of its environment, is


Code:
def redo(a):
    if a=='yes':
        print('OK!')
        return True
    return False
#...

    loop = redo(raw_input('question').lower())
__________________
[code]Code tags[/code] are essential for python code!

Last edited by b49P23TIvg : January 8th, 2013 at 07:35 AM. Reason: screwy!

Reply With Quote
  #3  
Old January 7th, 2013, 10:10 PM
iam1133 iam1133 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 2 iam1133 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 30 m 49 sec
Reputation Power: 0
Thank you, one more question

Thank you for your help! I have one question to your reply. When you type global before any variable, does that mean that the changes made to that variable within that function apply to the program as a whole? and without it, the changes made to that variable won't be applicable to the rest of the program?

Reply With Quote
  #4  
Old January 8th, 2013, 07:45 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,458 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 4 Days 6 h 26 m 43 sec
Reputation Power: 403
global & local scope

The output of these functions depends on the order in which you try them. The interesting experiment is, perhaps,
>>> G
'global'
>>> global_G()
global
>>> G
'changed'
Code:
G = 'global'

def no_assignment_python_finds_G_in_global_scope():
    print(G)

def UnboundLocal_error():
    '''
        G is local because it is assigned to within this function
    '''
    print(G)    # G doesn't yet have a value!
    G = 8       # assigment makes G local


def global_G():
    '''
        G in this function is the global G
    '''
    global G                             # tell python where to find G
    print(G)
    G = 'changed'

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Help with defined function and while loop

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap