473,399 Members | 3,302 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Horribly noobful string question

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.
Dec 13 '05 #1
4 1378
"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>

Dec 13 '05 #2
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
Dec 13 '05 #3

"Xavier Morel" <xa**********@masklinn.net> wrote in message
news:dn**********@aphrodite.grec.isp.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)
Dec 15 '05 #4
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)

Dec 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Stefan | last post by:
The following query gives me the right output, but takes almost 10 minutes to run... Any obvious things that I can improve on? I know this is diffuclt to do without realizign what I'm trying to...
2
by: sparks | last post by:
I know this is a stupid question but I can't find the answer. Please tell me where to find reference to this so I can print it out and staple it to my head.. dim I as integer dim missedstr as...
5
by: John Baro | last post by:
I have a richtextbox which I want the "literal" rtf of. richtextbox.rtf returns {\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033\\uc1 }\r\n\0 when i put this into a string I get...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
2
by: Dan Schumm | last post by:
I'm relatively new to regular expressions and was looking for some help on a problem that I need to solve. Basically, given an HTML string, I need to highlight certain words within the text of the...
53
by: Jeff | last post by:
In the function below, can size ever be 0 (zero)? char *clc_strdup(const char * CLC_RESTRICT s) { size_t size; char *p; clc_assert_not_null(clc_strdup, s); size = strlen(s) + 1;
6
by: RSH | last post by:
I am still trying to grasp the use of real world Objects and how to conceptualize them using a business scenerio. What I have below is an outline that I am wrestling with trying to figure out a...
0
by: | last post by:
I have a question about spawning and displaying subordinate list controls within a list control. I'm also interested in feedback about the design of my search application. Lots of code is at the...
2
by: robtyketto | last post by:
Greetings, Within my jsp I have HTML code (see below) which accepts input, one of these fields sequence unlike the others is an Integer. <FORM ACTION="wk465682AddFAQ.jsp" METHOD="POST"> Id:...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.