Some python newb help please?

Discussion in 'Python' started by lrwarren94@gmail.com, Nov 12, 2013.

  1. Guest

    So I'm trying to write a program for a problem in class, and something strange is happening that I can't figure out why is happening. I was wondering if you guys could help me fix it?

    http://pastebin.com/6QZTvx6Z

    Basically, 1 and 2 work just fine as inputs, but whenever I input 3 or 4, idle just doesn't do anything. Does anyone know why that is? any suggestions on how to fix? Any help is much appreciated :)
     
    , Nov 12, 2013
    #1
    1. Advertising

  2. Guest

    On Tuesday, November 12, 2013 4:21:58 PM UTC-6, Mark Lawrence wrote:
    > On 12/11/2013 22:14, wrote:
    >
    > > So I'm trying to write a program for a problem in class, and something strange is happening that I can't figure out why is happening. I was wondering if you guys could help me fix it?

    >
    > >

    >
    > > http://pastebin.com/6QZTvx6Z

    >
    > >

    >
    > > Basically, 1 and 2 work just fine as inputs, but whenever I input 3 or 4, idle just doesn't do anything. Does anyone know why that is? any suggestions on how to fix? Any help is much appreciated :)

    >
    > >

    >
    >
    >
    > Please put your code inline so we can see it, if it's too long see this
    >
    > http://sscce.org/ for advice.
    >
    >
    >
    > --
    >
    > Python is the second best programming language in the world.
    >
    > But the best has yet to be invented. Christian Tismer
    >
    >
    >
    > Mark Lawrence


    I'm not quite sure what you mean by that. it was on that pastebin link. I'll post it again here though. it's no longer than half a page.

    x = 0
    y = 0
    quitCommand = 0

    print "Welcome to the World of Textcraft!"
    print "----------------------------------"
    print ""

    while quitCommand != int(5):
    print "You are currently at (" + str(x) + ", " + str(y) + ")"
    print "Enter a command (1 = North, 2 = East, 3 = South, 4 = West, 5 = Exit):"
    if int(raw_input()) == 1:
    print "Moving north"
    y = y + 1
    elif int(raw_input()) == 2:
    print "Moving east"
    x = x + 1
    elif int(raw_input()) == 3:
    print "Moving south"
    y = y - 1
    elif int(raw_input()) == 4:
    print "Moving west"
    x = x - 1
    elif int(raw_input()) == 5:
    print "Dost thou leave so soon? Fare thee well!"
    quitCommand = 5
    else:
    print "I find your lack of reading comprehension skills disturbing."
     
    , Nov 12, 2013
    #2
    1. Advertising

  3. John Strick Guest

    Welcome to the world of Python programming! I'm glad you're learning this great language.

    As to your bug, think about this: in each if or elif statement, you're reading the user input again, so if user input is NOT equal to 1 in the first place, it reads input again. Try to step through your code mentally, or evenon paper, and track *exactly* what's happening. You could also add some print statements to see where you are. Good luck with your coding.

    Consider reading user input only once, and then checking it against the values 1,2,3,4, and 5.

    As an aside, "int(5)" is from the department of redundancy department. ;-)

    --John Strickler

    On Tuesday, November 12, 2013 5:14:42 PM UTC-5, wrote:
    > So I'm trying to write a program for a problem in class, and something strange is happening that I can't figure out why is happening. I was wondering if you guys could help me fix it?
    >
    >
    >
    > http://pastebin.com/6QZTvx6Z
    >
    >
    >
    > Basically, 1 and 2 work just fine as inputs, but whenever I input 3 or 4,idle just doesn't do anything. Does anyone know why that is? any suggestions on how to fix? Any help is much appreciated :)
     
    John Strick, Nov 12, 2013
    #3
  4. MRAB Guest

    On 12/11/2013 22:27, wrote:> On Tuesday, November
    12, 2013 4:21:58 PM UTC-6, Mark Lawrence wrote:
    >> On 12/11/2013 22:14, wrote:
    >>
    >> > So I'm trying to write a program for a problem in class, and

    something strange is happening that I can't figure out why is happening.
    I was wondering if you guys could help me fix it?
    >> >

    [snip]
    >
    > x = 0
    > y = 0
    > quitCommand = 0
    >
    > print "Welcome to the World of Textcraft!"
    > print "----------------------------------"
    > print ""


    You can simplify that to:

    print

    >
    > while quitCommand != int(5):


    5 is already an int, so int(5) == 5.

    > print "You are currently at (" + str(x) + ", " + str(y) + ")"
    > print "Enter a command (1 = North, 2 = East, 3 = South, 4 =

    West, 5 = Exit):"
    > if int(raw_input()) == 1:


    You're asking the user to enter something and then checking whether its
    int value is 1.

    > print "Moving north"
    > y = y + 1
    > elif int(raw_input()) == 2:


    Now you're asking the user to enter something _again_ and then checking
    whether its int value is 2.

    In other words, in order for it to print "Moving east" the following
    steps must occur:

    1. Ask the user to enter something.

    2. Check whether it's 1. It isn't. (Previous condition)

    3. Ask the user to enter something.

    4. Check whether it's 2. (This condition)

    > print "Moving east"
    > x = x + 1
    > elif int(raw_input()) == 3:


    Similar remarks to above, but longer.

    > print "Moving south"
    > y = y - 1
    > elif int(raw_input()) == 4:


    Similar remarks to above, but longer again.

    > print "Moving west"
    > x = x - 1
    > elif int(raw_input()) == 5:


    Similar remarks to above, but longer again.

    > print "Dost thou leave so soon? Fare thee well!"
    > quitCommand = 5
    > else:
    > print "I find your lack of reading comprehension skills

    disturbing."
    >


    The fix is simple. Ask once:

    answer = int(raw_input())
    if answer == 1:
    ...
    elif answer == 2:
    ...
    ...
     
    MRAB, Nov 12, 2013
    #4
  5. Tim Chase Guest

    On 2013-11-12 14:27, wrote:
    > if int(raw_input()) == 1:
    > print "Moving north"
    > y = y + 1
    > elif int(raw_input()) == 2:
    > print "Moving east"
    > x = x + 1
    > elif int(raw_input()) == 3:
    > print "Moving south"
    > y = y - 1
    > elif int(raw_input()) == 4:
    > print "Moving west"
    > x = x - 1
    > elif int(raw_input()) == 5:
    > print "Dost thou leave so soon? Fare thee well!"
    > quitCommand = 5
    > else:
    > print "I find your lack of reading comprehension skills
    > disturbing."


    Note that you're asking for input with each comparison. Best to get
    the input once and store it in a variable before the "if" statement
    and then do the comparisons against the same value.

    -tkc
     
    Tim Chase, Nov 12, 2013
    #5
  6. Guest

    On Tuesday, November 12, 2013 4:56:35 PM UTC-6, MRAB wrote:
    > On 12/11/2013 22:27, wrote:> On Tuesday, November
    >
    > 12, 2013 4:21:58 PM UTC-6, Mark Lawrence wrote:
    >
    > >> On 12/11/2013 22:14, wrote:

    >
    > >>

    >
    > >> > So I'm trying to write a program for a problem in class, and

    >
    > something strange is happening that I can't figure out why is happening.
    >
    > I was wondering if you guys could help me fix it?
    >
    > >> >

    >
    > [snip]
    >
    > >

    >
    > > x = 0

    >
    > > y = 0

    >
    > > quitCommand = 0

    >
    > >

    >
    > > print "Welcome to the World of Textcraft!"

    >
    > > print "----------------------------------"

    >
    > > print ""

    >
    >
    >
    > You can simplify that to:
    >
    >
    >
    > print
    >
    >
    >
    > >

    >
    > > while quitCommand != int(5):

    >
    >
    >
    > 5 is already an int, so int(5) == 5.
    >
    >
    >
    > > print "You are currently at (" + str(x) + ", " + str(y) + ")"

    >
    > > print "Enter a command (1 = North, 2 = East, 3 = South, 4 =

    >
    > West, 5 = Exit):"
    >
    > > if int(raw_input()) == 1:

    >
    >
    >
    > You're asking the user to enter something and then checking whether its
    >
    > int value is 1.
    >
    >
    >
    > > print "Moving north"

    >
    > > y = y + 1

    >
    > > elif int(raw_input()) == 2:

    >
    >
    >
    > Now you're asking the user to enter something _again_ and then checking
    >
    > whether its int value is 2.
    >
    >
    >
    > In other words, in order for it to print "Moving east" the following
    >
    > steps must occur:
    >
    >
    >
    > 1. Ask the user to enter something.
    >
    >
    >
    > 2. Check whether it's 1. It isn't. (Previous condition)
    >
    >
    >
    > 3. Ask the user to enter something.
    >
    >
    >
    > 4. Check whether it's 2. (This condition)
    >
    >
    >
    > > print "Moving east"

    >
    > > x = x + 1

    >
    > > elif int(raw_input()) == 3:

    >
    >
    >
    > Similar remarks to above, but longer.
    >
    >
    >
    > > print "Moving south"

    >
    > > y = y - 1

    >
    > > elif int(raw_input()) == 4:

    >
    >
    >
    > Similar remarks to above, but longer again.
    >
    >
    >
    > > print "Moving west"

    >
    > > x = x - 1

    >
    > > elif int(raw_input()) == 5:

    >
    >
    >
    > Similar remarks to above, but longer again.
    >
    >
    >
    > > print "Dost thou leave so soon? Fare thee well!"

    >
    > > quitCommand = 5

    >
    > > else:

    >
    > > print "I find your lack of reading comprehension skills

    >
    > disturbing."
    >
    > >

    >
    >
    >
    > The fix is simple. Ask once:
    >
    >
    >
    > answer = int(raw_input())
    >
    > if answer == 1:
    >
    > ...
    >
    > elif answer == 2:
    >
    > ...
    >
    > ...


    Thanks a lot! I'll try this out!
    Sorry to everyone else whose eyes I made bleed. I've never used a newsgroup before...still not really sure what they are. Found this through a google search :\
     
    , Nov 12, 2013
    #6
  7. On Tue, 12 Nov 2013 14:14:42 -0800, lrwarren94 wrote:

    > http://pastebin.com/6QZTvx6Z


    Work through your code very very carefully. You're doing something in
    each if branch that you probably only want to do once in each execution
    of the while loop.

    If you can't figure it out, I'll post a corrected version next week.

    --
    Denis McMahon,
     
    Denis McMahon, Nov 12, 2013
    #7
  8. On Wed, Nov 13, 2013 at 10:04 AM, <> wrote:
    > Thanks a lot! I'll try this out!
    > Sorry to everyone else whose eyes I made bleed. I've never used a newsgroup before...still not really sure what they are. Found this through a google search :\


    There's an easy fix. Go to this page:

    https://mail.python.org/mailman/listinfo/python-list

    You can then subscribe to the mailing list using whatever email
    address you're comfortable with. I use Gmail, which works fairly well
    but has its own issues; I've glanced at Evolution (a Linux mail
    client) and its way of handling threads, and it seems to do a good
    job. Plenty of options.

    ChrisA
     
    Chris Angelico, Nov 12, 2013
    #8
  9. On Tue, 12 Nov 2013 14:27:37 -0800 (PST), declaimed
    the following:


    >
    >I'm not quite sure what you mean by that. it was on that pastebin link. I'll post it again here though. it's no longer than half a page.


    The preference here is that if you ask a question about code, you
    provide the code IN the message -- you don't send us off to some site that
    may or may not retain the code down the road (whereas the question posted
    here may be archived forever)

    >
    >x = 0
    >y = 0
    >quitCommand = 0
    >
    >print "Welcome to the World of Textcraft!"
    >print "----------------------------------"
    >print ""
    >
    >while quitCommand != int(5):


    5 IS an int, so why coerce it to itself.

    > print "You are currently at (" + str(x) + ", " + str(y) + ")"
    > print "Enter a command (1 = North, 2 = East, 3 = South, 4 = West, 5 = Exit):"
    > if int(raw_input()) == 1:
    > print "Moving north"
    > y = y + 1
    > elif int(raw_input()) == 2:
    > print "Moving east"
    > x = x + 1
    > elif int(raw_input()) == 3:
    > print "Moving south"
    > y = y - 1
    > elif int(raw_input()) == 4:
    > print "Moving west"
    > x = x - 1
    > elif int(raw_input()) == 5:
    > print "Dost thou leave so soon? Fare thee well!"
    > quitCommand = 5
    > else:
    > print "I find your lack of reading comprehension skills disturbing."
    >


    Note that you ask for NEW input from the user on each if/elif. You want
    to read ONE value from the user, and then compare THAT value on each
    if/elif line.

    HINT: raw_input() can display a prompt string... so

    command = int(raw_input(
    "Enter a command "
    "(1 = North, 2 = East, 3 = South, 4 = West, 5 = Exit): "))

    {Note: I took advantage of Python's ability to concatenate adjacent strings
    -- that way the line doesn't wrap at odd locations in UseNet}
    --
    Wulfraed Dennis Lee Bieber AF6VN
    HTTP://wlfraed.home.netcom.com/
     
    Dennis Lee Bieber, Nov 13, 2013
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. KK
    Replies:
    2
    Views:
    709
    Big Brian
    Oct 14, 2003
  2. Apotheosis

    Some Newb Problem with "int", please help.

    Apotheosis, Sep 20, 2004, in forum: C Programming
    Replies:
    24
    Views:
    609
    Alex Monjushko
    Sep 23, 2004
  3. isaac2004
    Replies:
    4
    Views:
    396
    =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=
    Oct 4, 2007
  4. jflo
    Replies:
    0
    Views:
    290
  5. Karla Chubb

    Im a newb so please help!

    Karla Chubb, Jul 22, 2006, in forum: Ruby
    Replies:
    4
    Views:
    127
    Simon Harrison
    Jul 22, 2006
Loading...

Share This Page