Thread: Fibonacci
View Single Post
Old Apr 28th, 2005, 1:50 PM   #4
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
You're reading it in as a string - you need to convert it to an integer if you want to use it as one.

To be honest, the int function isn't ideal - it'll break if someone types in "My name is sploo.", for example. I wrote a function a while back to take care of this - here ye go:
################################################################################
#                                                                              #
#                                   str2num                                    #
#                                                                              #
#                                  by  Ooble                                   #
#                                                                              #
# str2num is a simple string-to-number converter. It takes a string and        #
# filters out all the crap - letters, weird characters and extra decimal       #
# points. It also lets you specify the return type. Unfortunately, you'll      #
# probably need to know basic Python to understand this.                       #
#                                                                              #
# The code is heavily commented - enjoy.                                       #
#                                                                              #
################################################################################

def str2num (str, retType = ""):
	decimal = 0	# this is the flag telling you whether
				# there's already a decimal point
	i = 0		# our loop variable
	
	while i < len(str):
		# if str[i] is not a digit or a decimal point,
		# or is a decimal point but we already have one
		if (not(str[i].isdigit() or str[i] == ".")
			or ((str[i] == ".") and decimal)):
			# remove it from the string
			str = str[:i] + str[i+1:]
		
		# if it's a decimal point and there isn't one already
		elif (str[i] == ".") and not(decimal):
			decimal = 1	# change the flag to true
			i = i + 1	# next letter
		
		# anything else - i.e. numbers
		else:
			i = i + 1	# next letter
	
	# only do this if there's anything in the string
	if len(str) > 0:
		# if there's a decimal point at the front, add a 0 in front
		if str[0] == ".":
			str = "0" + str[:]
		
		# if the last character is a decimal point, remove it and change the
		# flag back to false
		if str[-1] == ".":
			str = str[:-1]
			decimal = 0
	
	# if there's nothing in the string, make it 0
	if len(str) == 0:
		str = "0"
	
	# if the specified return type is "string" or "str", return it as a string
	if (retType == "string") or (retType == "str"):
		return str
	# if it's "float", return it as a floating-point number
	elif retType == "float":
		return float(str)
	# if it's "int" or "integer", return it as a long (4-byte) integer
	elif (retType == "int") or (retType == "integer"):
		return long(str)
	# otherwise, take a guess
	else:
		if decimal:
			return float(str)
		else:
			return long(str)
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote