On 7 August 2013 09:17, <[email protected]> wrote:
> I'm trying to create an option for the program to repeat if the user types
> 'y' or 'yes', using true and false values, or otherwise end the program. If
> anyone could explain to me how to get this code working, I'd appreciate it.
Always tell people what in particular you don't understand (*ducks*)
because it wasn't obvious what part of the problem you were unable to
fulfil.
> letters='abcdefghijklmn'
> batman=True
> def thingy():
> print('type letter from a to n')
> typedletter=input()
> if typedletter in letters:
> print('yes')
> else:
> print('no')
> def repeat():
> print('go again?')
> goagain=input()
> if goagain in ('y', 'yes'):
> print('ok')
> else:
> print('goodbye')
> batman=False
This doesn't do what you want it to.
x = "old thing"
def change_x():
x = "new thing"
change_x()
print(x) # Not changed!
The solution is to put "global x" at the start of the function.
> while batman==True:
> thingy()
> repeat()
> print('this is the end')
Note that this isn't actually a good way to do it. Imagine you had
several hundred function -- would you really want to have an
equivalent number of names floating around that you have to look
after?
The solution is to make the functions simply return values:
x = "old thing"
def return_thing():
x = "new thing"
return "new thing" # You can do this in one line
x = return_thing() # Get the value from the function and change x with it
Does this make sense?
--
http://mail.python.org/mailman/listinfo/python-list