![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
|
Catching only one word
Using this code to get a sequence of 'words' (well, numbers, but that's irrelevant) separated by a space, it fails at the split(" ") when only one word is given (I assume). However, it doesn't raise an exception and I'm unsure on how to catch the single word anyway.
def GetSequence(highNumber):
while 1:
sequence = GetString("Enter a Sequence of numbers separated by a space: ")
try:
list = []
for word in sequence.split(" "):
list.append(int(word))
except ValueError:
print "Error: '%s' is not a number! Try again." % word,
continue
except:
print "Error! Try again.",
continue
break
return listLast edited by UnKnown X; Feb 11th, 2006 at 2:17 PM. Reason: Typo |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
|
Found a solution!
def GetSequence(highNumber):
while 1:
sequence = GetString("Enter a Sequence of numbers separated by a space: ")
try:
list = []
try:
list.append(int(sequence))
except ValueError:
for word in sequence.split(" "):
list.append(int(word))
except:
print "Error! Try again.",
continue
except ValueError:
print "Error: '%s' is not a number! Try again." % word,
continue
except:
print "Error! Try again.",
continue
break
return listI love quick fixes! ![]() |
|
|
|
|
|
#3 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
We appreciate your posting the solution for other people. All too often people don't do that, or destroy their post, or something. Thanks.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
|
Glad to help! (Even though I probably mostly helped myself here...
) |
|
|
|
|
|
#5 | |
|
Programmer
Join Date: Apr 2005
Posts: 73
Rep Power: 4
![]() |
Quote:
x = "1234"
print x.split(" ") # Will be a single-element list.def get_sequence():
while True:
seq = raw_input("Enter a sequence of numbers separated by a space: ")
try:
return [int(word) for word in seq.split(" ")]
except ValueError:
print "Error: '%s' is not a number! Try again." % word
print get_sequence()* functions and variables use lower_case; classes use MixedCase. * high_number was unused * "while True" is a little prettier than while 1 * It's easier to build a list comprehension than manually create and append elements. * Never call a list "list" since it shadows a built-in. * The only exception that the return... line is likely to raise is ValueError. Any other exception is a critical one (eg: MemoryError, in which case continuing is not a great idea) * bare excepts shouldn't be used in 99% of cases. -T. |
|
|
|
|
|
|
#6 |
|
Hobbyist Programmer
|
Thanks for your post, hydroxide!
I don't think Get_string is doing anything wrong - it does use raw_input, just with error checking. I just put it in a separate function to keep it a bit neat. Here it is anyway: def Get_sequence():
while True:
sequence = Get_string("Enter a Sequence of numbers separated by a space: ")
if sequence == None:
return None
try:
token_list = []
try:
token_list.append(int(sequence))
except ValueError:
for word in sequence.split(" "):
token_list.append(int(word))
except:
print "Error! Try again.",
return None
except ValueError:
print "Error: '%s' is not a number! Try again." % word,
continue
except:
print "Error!",
return None
break
return token_list
def Get_string(msg):
while True:
try:
return raw_input(msg)
except ValueError:
print "ValueError exception encountered! Try again."
continue
except:
print "Error!"
return None
break
print "Loop error occured!"
return None |
|
|
|
|
|
#7 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Quote:
def parse_sequence(sequence):
output = []
for item in sequence.split(" "):
try:
output.append(int(item))
except ValueError:
raise ValueError, "'%s' is not a number! Try again." % item
return output
def get_user_sequence(prompt):
return parse_sequence(raw_input(prompt))
def printer(s):
print s
def while_exceptions(function, logger = printer):
while True:
try:
return function()
except Exception, e:
logger(e)
def main():
def get_sequence():
return get_user_sequence(
"Enter a sequence of numbers separated by a space: ")
print while_exceptions(get_sequence, printer)
main() |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|