![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Apr 2005
Posts: 17
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#2 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Try this:
newnumb = int(fib) |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Apr 2005
Posts: 17
Rep Power: 0
![]() |
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?
|
|
|
|
|
|
#4 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
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) |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Apr 2005
Posts: 17
Rep Power: 0
![]() |
Okej, thanks alot!
|
|
|
|
|
|
#6 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
It's a long bugger, but it takes into account every single possible input, and returns a valid number for all of them.
|
|
|
|
|
|
#7 |
|
Programming Guru
![]() |
were you bored one day?
|
|
|
|
|
|
#8 |
|
Newbie
Join Date: Apr 2005
Posts: 17
Rep Power: 0
![]() |
Hehe, I guess so
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|