Horribly noobful string question

Discussion in 'Python' started by SeNTry, Dec 13, 2005.

  1. SeNTry

    SeNTry Guest

    Hi Everyone,

    My first post here as I just begin to learn programming in general and
    python in particular. I have all the noobie confused questions, but as I
    work thru the tutorials I'm sure I'll find most my answers.

    This one is eluding me tho... I am working in the tutorials, writing scripts
    as presented and then modifying and expanding on my own to try to learn.
    I'm working with one that asks the user to 'guess a number I'm thinking',
    and with simple while loop, flow control and operands, returning an answer
    to guess again or you got it. I've added a 'playagain' function I've got
    working, but what I want is to stop the program from crashing when someone
    enters a string value instead of a int value. I know strings are immutable,
    and they can be changed to an int equivalent, but I just want the script to
    recognize the input as a string and print a simple "that's not a number, try
    again' type of message. I can't find the syntax to include in the
    if/elif/else block to include a line that says something like,

    elif guess == <string>
    print "that's not a number! please guess again!"

    I know that's not right, but can you see what I'm looking for and offer a
    suggestion?

    Thanks in advance all.
     
    SeNTry, Dec 13, 2005
    #1
    1. Advertising

  2. "SeNTry" wrote:

    > My first post here as I just begin to learn programming in general and
    > python in particular. I have all the noobie confused questions, but as I
    > work thru the tutorials I'm sure I'll find most my answers.
    >
    > This one is eluding me tho... I am working in the tutorials, writing scripts
    > as presented and then modifying and expanding on my own to try to learn.
    > I'm working with one that asks the user to 'guess a number I'm thinking',
    > and with simple while loop, flow control and operands, returning an answer
    > to guess again or you got it. I've added a 'playagain' function I've got
    > working, but what I want is to stop the program from crashing when someone
    > enters a string value instead of a int value. I know strings are immutable,
    > and they can be changed to an int equivalent, but I just want the script to
    > recognize the input as a string and print a simple "that's not a number, try
    > again' type of message. I can't find the syntax to include in the
    > if/elif/else block to include a line that says something like,


    assuming you're using raw_input() to get the guess, you always
    have a string (in python's sense of that word).

    what you seem to want is to check if the string contains a number
    or not. here's one way to do this:

    guess = raw_input("make a guess: ")
    if guess == secret:
    print "congratulations!"
    elif not guess.isdigit():
    print "that's not a number! please guess again!"
    ...

    isdigit returns true if the string contains nothing but digits:

    >>> help(str.isdigit)


    isdigit(...)
    S.isdigit() -> bool

    Return True if there are only digit characters in S,
    False otherwise.

    if you're using some other way to read user input, let us know.

    </F>
     
    Fredrik Lundh, Dec 13, 2005
    #2
    1. Advertising

  3. SeNTry

    Xavier Morel Guest

    Fredrik Lundh wrote:
    > "SeNTry" wrote:
    >
    >> My first post here as I just begin to learn programming in general and
    >> python in particular. I have all the noobie confused questions, but as I
    >> work thru the tutorials I'm sure I'll find most my answers.
    >>
    >> This one is eluding me tho... I am working in the tutorials, writing scripts
    >> as presented and then modifying and expanding on my own to try to learn.
    >> I'm working with one that asks the user to 'guess a number I'm thinking',
    >> and with simple while loop, flow control and operands, returning an answer
    >> to guess again or you got it. I've added a 'playagain' function I've got
    >> working, but what I want is to stop the program from crashing when someone
    >> enters a string value instead of a int value. I know strings are immutable,
    >> and they can be changed to an int equivalent, but I just want the script to
    >> recognize the input as a string and print a simple "that's not a number, try
    >> again' type of message. I can't find the syntax to include in the
    >> if/elif/else block to include a line that says something like,

    >
    > assuming you're using raw_input() to get the guess, you always
    > have a string (in python's sense of that word).
    >
    > what you seem to want is to check if the string contains a number
    > or not. here's one way to do this:
    >
    > guess = raw_input("make a guess: ")
    > if guess == secret:
    > print "congratulations!"
    > elif not guess.isdigit():
    > print "that's not a number! please guess again!"
    > ...
    >


    that, or just write something like

    guess = raw_input("Make your guess > ")
    try:
    if int(guess) == secret:
    # ok
    except ValueError:
    # no good
     
    Xavier Morel, Dec 13, 2005
    #3
  4. SeNTry

    SeNTry Guest

    "Xavier Morel" <> wrote in message
    news:dnn2g8$l12$.9tel.net...
    > Fredrik Lundh wrote:
    >> "SeNTry" wrote:
    >>
    >>> My first post here as I just begin to learn programming in general and
    >>> python in particular. I have all the noobie confused questions, but as
    >>> I
    >>> work thru the tutorials I'm sure I'll find most my answers.
    >>>
    >>> This one is eluding me tho... I am working in the tutorials, writing
    >>> scripts
    >>> as presented and then modifying and expanding on my own to try to learn.
    >>> I'm working with one that asks the user to 'guess a number I'm
    >>> thinking',
    >>> and with simple while loop, flow control and operands, returning an
    >>> answer
    >>> to guess again or you got it. I've added a 'playagain' function I've
    >>> got
    >>> working, but what I want is to stop the program from crashing when
    >>> someone
    >>> enters a string value instead of a int value. I know strings are
    >>> immutable,
    >>> and they can be changed to an int equivalent, but I just want the script
    >>> to
    >>> recognize the input as a string and print a simple "that's not a number,
    >>> try
    >>> again' type of message. I can't find the syntax to include in the
    >>> if/elif/else block to include a line that says something like,

    >>
    >> assuming you're using raw_input() to get the guess, you always
    >> have a string (in python's sense of that word).
    >>
    >> what you seem to want is to check if the string contains a number
    >> or not. here's one way to do this:
    >>
    >> guess = raw_input("make a guess: ")
    >> if guess == secret:
    >> print "congratulations!"
    >> elif not guess.isdigit():
    >> print "that's not a number! please guess again!"
    >> ...
    >>

    >
    > that, or just write something like
    >
    > guess = raw_input("Make your guess > ")
    > try:
    > if int(guess) == secret:
    > # ok
    > except ValueError:
    > # no good


    >>
    >> assuming you're using raw_input() to get the guess, you always
    >> have a string (in python's sense of that word).
    >>
    >> what you seem to want is to check if the string contains a number
    >> or not. here's one way to do this:
    >>
    >> guess = raw_input("make a guess: ")
    >> if guess == secret:
    >> print "congratulations!"
    >> elif not guess.isdigit():
    >> print "that's not a number! please guess again!"
    >> ...
    >>

    >
    > that, or just write something like
    >
    > guess = raw_input("Make your guess > ")
    > try:
    > if int(guess) == secret:
    > # ok
    > except ValueError:
    > # no good


    Sry for late reply, I've been out of town. Thanks for the responses, I'm
    just sitting down to try these out. I'm kind of surprised there's not a
    more obvious way to handle simply identifying strings.

    Anyways, here's the original code snippet from a tut. and then my modified
    effort. I was using input instead of raw_input. Looking at it now I'm not
    even sure why I did some of the stuff I did HAHA! I just made functions for
    convenience and practice. I'm sure it's laughable, but maybe you can see
    what I was doing and tell me what other errors I made just for learning...

    Everything seems to work well, except when the playagain function in my
    modified code gets a '2' input to quit, it prints 'aw, ok bye then' and then
    the next line is the print from the loopfunc if statement, "looping while
    statement now complete". If I remove the again="" line in the playagain
    function, it prints 2 times... wierd. I put this in there because I
    suspected that the variable was remaining and wanted to clear it at the
    start of the function, but I've now read that the variable in a function is
    destroyed when the function ends... is this right? My brain hurts...

    ORIGINAL
    number = 24
    guess = int(raw_input('Enter an integer : '))

    if guess == number:
    print 'Congratulations, you guessed it.' # New block starts here
    print "(but you do not win any prizes!)" # New block ends here
    elif guess < number:
    print 'No, it is a little higher than that' # Another block
    # You can do whatever you want in a block ...
    else:
    print 'No, it is a little lower than that'
    # you must have guess > number to reach here

    print 'Done'
    # This last statement is always executed, after the if statement is
    executed**MODIFIED**#define two functions first, then use them.def
    loopfunc(looping): while looping: guess= input("guess a number.
    see if you can guess what I'm thinking") if guess == number:
    print "you got it!" looping=False playagain("")
    print "looping while statement now complete" #for clarification when running
    elif guess < number: print "nope, a little higher!" else:
    print "no, a little lower!"def playagain(again): again="" #removing
    this line make the 'looping while..' statement print 2 times again=
    input("would you like to play again? type '1' for yes and '2' for no") if
    again==1: print "great!" loopfunc(True) elif again==2:
    print "aww! Ok, bye then" return else: print "that's not a
    1 or a 2! Try again!" playagain("")number=24loopfunc(True)
     
    SeNTry, Dec 15, 2005
    #4
  5. SeNTry

    SeNTry Guest

    SRY, that last bit of code got messed up. Hopefully it will look right
    now...

    #define two functions first, then use them.

    def loopfunc(looping):
    while looping:
    guess= input("guess a number. see if you can guess what I'm thinking")
    if guess == number:
    print "you got it!"
    looping=False
    playagain("")
    print "looping while statement now complete"
    elif guess < number:
    print "nope, a little higher!"
    else:
    print "no, a little lower!"

    def playagain(again):
    again=""
    again= input("would you like to play again? type '1' for yes and '2' for
    no")
    if again==1:
    print "great!"
    loopfunc(True)
    elif again==2:
    print "aww! Ok, bye then"
    return
    else:
    print "that's not a 1 or a 2! Try again!"
    playagain("")
    number=24
    loopfunc(True)

    >
    >
     
    SeNTry, Dec 15, 2005
    #5
    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. Mladen Adamovic
    Replies:
    0
    Views:
    739
    Mladen Adamovic
    Dec 4, 2003
  2. Mladen Adamovic
    Replies:
    3
    Views:
    14,614
    Mladen Adamovic
    Dec 5, 2003
  3. Ara.T.Howard

    horribly impossible debugging task

    Ara.T.Howard, Sep 16, 2004, in forum: Ruby
    Replies:
    11
    Views:
    416
  4. Doug Livesey
    Replies:
    2
    Views:
    150
    Santhoshkumar Muniraj
    Nov 16, 2009
  5. Chris Angelico

    Python is horribly slow compared to bash!!

    Chris Angelico, May 22, 2014, in forum: Python
    Replies:
    6
    Views:
    68
    Dennis Lee Bieber
    May 28, 2014
Loading...

Share This Page