Python Forum
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
first function
#2
It is a function, sort of, but it doesn't do anything. It doesn't produce a result.

Do not define a function inside a while loop. It is not an error to do so, but it is not where I would expect to find a function definition. Functions are usually defined near the top of a module. fibo() should appear above "while True:" in the file.

Don't use global variables to pass information to a function. Use function arguments.

Your function doesn't return a value. It calculates numbers, but nothing is done, or can be done, with those numbers. I would write this function as a generator, but maybe you should start out with returning the last number in the sequence or a list of the numbers.

Why are you limiting howMany to be > 5?
fibo(0) : None? Empty list?
fibo(1) : 1
fibo(2) : 1 1
fibo(3) : 1 1 2
fibo(4) : 1 1 2 3

Why are you using a while loop to compute the numbers? You know how many times the loop needs to run. Use "for _ in range(howMany):". Let python do the loop counting for you.

Why are there 3 variables, a, b, c? I know you are using c as a temporary swap variable to hold the sum of a+b, but you don't need swap variables in Python. In python you can do this:
a, b = b, a+b
which does the same as:
c = a + b
a = b
b = c
Reply


Messages In This Thread
first function - by astral_travel - Nov-12-2022, 06:49 PM
RE: first function - by deanhystad - Nov-13-2022, 05:36 PM
RE: first function - by astral_travel - Nov-13-2022, 06:20 PM
RE: first function - by ndc85430 - Nov-13-2022, 06:33 PM
RE: first function - by astral_travel - Nov-13-2022, 07:17 PM
RE: first function - by ndc85430 - Nov-13-2022, 08:37 PM
RE: first function - by deanhystad - Nov-15-2022, 04:45 PM
RE: first function - by astral_travel - Nov-13-2022, 08:40 PM
RE: first function - by deanhystad - Nov-13-2022, 08:59 PM
RE: first function - by Pedroski55 - Nov-13-2022, 11:48 PM
RE: first function - by snippsat - Nov-14-2022, 12:50 AM
RE: first function - by jefsummers - Nov-14-2022, 01:42 PM
RE: first function - by astral_travel - Nov-15-2022, 06:09 PM
RE: first function - by astral_travel - Nov-21-2022, 08:01 PM
RE: first function - by rob101 - Nov-21-2022, 08:13 PM
RE: first function - by astral_travel - Nov-21-2022, 08:29 PM
RE: first function - by rob101 - Nov-21-2022, 08:40 PM
RE: first function - by astral_travel - Nov-21-2022, 08:55 PM
RE: first function - by rob101 - Nov-21-2022, 09:22 PM
RE: first function - by astral_travel - Nov-21-2022, 09:24 PM
RE: first function - by rob101 - Nov-21-2022, 09:35 PM
RE: first function - by astral_travel - Nov-21-2022, 10:01 PM
RE: first function - by rob101 - Nov-21-2022, 10:32 PM
RE: first function - by deanhystad - Nov-21-2022, 11:38 PM
RE: first function - by rob101 - Nov-22-2022, 09:35 AM
RE: first function - by Larz60+ - Nov-22-2022, 02:51 AM
RE: first function - by astral_travel - Nov-22-2022, 06:39 PM
RE: first function - by rob101 - Nov-22-2022, 07:15 PM
RE: first function - by astral_travel - Nov-22-2022, 07:19 PM
RE: first function - by rob101 - Nov-22-2022, 07:33 PM
RE: first function - by astral_travel - Nov-22-2022, 08:20 PM
RE: first function - by Yoriz - Nov-22-2022, 08:35 PM
RE: first function - by rob101 - Nov-22-2022, 08:38 PM
RE: first function - by deanhystad - Nov-22-2022, 10:51 PM
RE: first function - by astral_travel - Nov-23-2022, 06:43 PM
RE: first function - by deanhystad - Nov-24-2022, 04:04 AM
RE: first function - by astral_travel - Nov-24-2022, 04:22 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020