The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Help with defined function and while loop
Discuss Help with defined function and while loop in the Python Programming forum on Dev Shed. Help with defined function and while loop Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 7th, 2013, 05:42 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 2
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?
|

January 7th, 2013, 07:22 PM
|
 |
Contributing User
|
|
|
|
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!
|

January 7th, 2013, 10:10 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 2
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?
|

January 8th, 2013, 07:45 AM
|
 |
Contributing User
|
|
|
|
|
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'
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|