Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #52121

Re: beginner question (True False help)

References <6c6dedec-5e47-4229-bc67-01b058cdb410@googlegroups.com>
From Joshua Landau <joshua@landau.ws>
Date 2013-08-07 09:42 +0100
Subject Re: beginner question (True False help)
Newsgroups comp.lang.python
Message-ID <mailman.306.1375865009.1251.python-list@python.org> (permalink)

Show all headers | View raw


On 7 August 2013 09:17,  <eschneider92@comcast.net> 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?

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

beginner question (True False help) eschneider92@comcast.net - 2013-08-07 01:17 -0700
  Re: beginner question (True False help) Joshua Landau <joshua@landau.ws> - 2013-08-07 09:42 +0100
  Re: beginner question (True False help) eschneider92@comcast.net - 2013-08-07 13:59 -0700
    Re: beginner question (True False help) Dave Angel <davea@davea.name> - 2013-08-08 01:18 +0000
  Re: beginner question (True False help) Larry Hudson <orgnut@yahoo.com> - 2013-08-07 19:49 -0700
  Re: beginner question (True False help) wxjmfauth@gmail.com - 2013-08-07 23:20 -0700
    Re: beginner question (True False help) Chris Angelico <rosuav@gmail.com> - 2013-08-08 12:41 +0100
    Re: beginner question (True False help) Terry Reedy <tjreedy@udel.edu> - 2013-08-08 16:29 -0400
      Re: beginner question (True False help) wxjmfauth@gmail.com - 2013-08-09 01:05 -0700
  Re: beginner question (True False help) eschneider92@comcast.net - 2013-08-09 15:27 -0700
    Re: beginner question (True False help) Joshua Landau <joshua@landau.ws> - 2013-08-10 00:05 +0100
    Re: beginner question (True False help) Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-08-09 22:58 -0400
  Re: beginner question (True False help) eschneider92@comcast.net - 2013-08-09 15:28 -0700
  Re: beginner question (True False help) eschneider92@comcast.net - 2013-08-09 16:14 -0700
    Re: beginner question (True False help) Joshua Landau <joshua@landau.ws> - 2013-08-10 00:30 +0100
  Re: beginner question (True False help) eschneider92@comcast.net - 2013-08-09 16:24 -0700
  Re: beginner question (True False help) eschneider92@comcast.net - 2013-08-09 16:34 -0700
    Re: beginner question (True False help) Joshua Landau <joshua@landau.ws> - 2013-08-10 02:22 +0100
    Re: beginner question (True False help) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-08-10 01:40 +0000
  Re: beginner question (True False help) eschneider92@comcast.net - 2013-08-09 16:40 -0700
    Re: beginner question (True False help) MRAB <python@mrabarnett.plus.com> - 2013-08-10 01:39 +0100
  Re: beginner question (True False help) eschneider92@comcast.net - 2013-08-09 16:43 -0700
  Re: beginner question (True False help) eschneider92@comcast.net - 2013-08-09 18:08 -0700

csiph-web