Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 27th, 2005, 12:59 PM   #1
lingon
Newbie
 
Join Date: Apr 2005
Posts: 17
Rep Power: 0 lingon is on a distinguished road
Fibonacci

Hey!
I am trying to make a program that calculates the fibonacci series up to a cetain value and it worked fine but when I tried to add a feature that asks you to type a number and then calculate the serie up to that number it jst keeps going and going. I don't think that it understands that it is actually a number that I type. Have a look.

fib = raw_input("Skriv en siffra:")
newnumb = fib 
def fib(newnumb):
    a, b = 0, 1
    while b < newnumb:
         print b,
         a, b = b, a+b
fib(newnumb)

Last edited by lingon; Apr 27th, 2005 at 1:03 PM.
lingon is offline   Reply With Quote
Old Apr 27th, 2005, 1:14 PM   #2
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
Try this:
newnumb = int(fib)
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Apr 28th, 2005, 12:00 PM   #3
lingon
Newbie
 
Join Date: Apr 2005
Posts: 17
Rep Power: 0 lingon is on a distinguished road
Thanks! It works fine now but exactly what does newnumb = int(fib) tell python to do? I mean what does newnumb mean and why does it work with int and not without?
lingon is offline   Reply With Quote
Old Apr 28th, 2005, 12: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
Old Apr 28th, 2005, 3:38 PM   #5
lingon
Newbie
 
Join Date: Apr 2005
Posts: 17
Rep Power: 0 lingon is on a distinguished road
Okej, thanks alot!
lingon is offline   Reply With Quote
Old Apr 28th, 2005, 3:58 PM   #6
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
It's a long bugger, but it takes into account every single possible input, and returns a valid number for all of them.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Apr 28th, 2005, 4:16 PM   #7
Berto
Programming Guru
 
Join Date: Aug 2004
Posts: 1,022
Rep Power: 6 Berto is on a distinguished road
Send a message via AIM to Berto Send a message via MSN to Berto
were you bored one day?
Berto is offline   Reply With Quote
Old Apr 29th, 2005, 11:35 AM   #8
lingon
Newbie
 
Join Date: Apr 2005
Posts: 17
Rep Power: 0 lingon is on a distinguished road
Hehe, I guess so
lingon is offline   Reply With Quote
Old Apr 29th, 2005, 6:22 PM   #9
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
I needed it for an app I was writing (which promptly collapsed).
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:50 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC