beginner question (True False help)

Joshua Landau joshua at landau.ws
Fri Aug 9 19:05:17 EDT 2013


On 9 August 2013 23:27,  <eschneider92 at comcast.net> wrote:
> This is what I ended up with btw. Does this insult anyone's more well attuned Python sensibilities?

...

Yes.

You didn't listen to any of the advice we've been giving you. You've
had *much* better answers given than this.


Start from the top.

We need letters, so define that:

    letters = "abcdefghijkl"

We then want to loop, possibly forever. A good choice is a "while" loop.

    # True is always True, so will loop forever
    while True:

We then want to ask for a letter. We want to use "input". Write
"help(input)" in the Python Shell and you get

    >>> help(input)
    Help on built-in function input in module builtins:

    input(...)
        input([prompt]) -> string

        Read a string from standard input.  The trailing newline is stripped.
        If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return),
raise EOFError.
        On Unix, GNU readline is used if enabled.  The prompt string, if given,
        is printed without a trailing newline before reading.

So our line should be:

    letter = input("Type a letter from 'a' to 'n' in the alphabet: ")

Then we want to test if it's on of our letters:

    if letter in letters:

And if so we want to say something positive:

        print("That's right.")

If not we want to say something negative:

    else:
        print("That's wrong.")

And then we want to ask if we should go again:

    go_again = input("Do you want to do this again? ")

If the response is "y" or "yes", we want to continue looping. The
while loop will do that automatically, so we can do nothing in this
circumstance.

If the response in *not* "y" or "yes", we want to stop:

    if go_again not in ("y", "yes"):
       break


That's it. No need to complicate things. Just take it one step at a time.



More information about the Python-list mailing list