Hack Forums
[Python] python fibonacci sequence error and other questions on python - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] python fibonacci sequence error and other questions on python (/showthread.php?tid=5725453)



python fibonacci sequence error and other questions on python - fogstone - Sep 26, 2017

Hey everyone, as you've probably inferred, I'm taking another integrated programming class. I wanted to ask about some basic information about a python program I am doing for practice (not from the class itself). I want to be told what I am doing wrong without giving away the answer please. Here is my python code:

Code
list = [range(0,100)]
for num in list:
    if num == 0:
        i = 0
        print(str(i) + " ")
    elif num == 1:
        i = 1
        print(str(i) + " ")
    else:
        i = (list[num - 1] + list[num - 2])
        print(str(i) + " ")

I get this error and I didn't think it would happen:

Code
PS C:\Users\William Smith\Desktop\New CIT 270\Python> python .\exercise13.py
Traceback (most recent call last):
  File ".\exercise13.py", line 10, in <module>
    i = (list[num - 1] + list[num - 2])
TypeError: unsupported operand type(s) for -: 'range' and 'int'

Again could someone tell me what I'm doing wrong without explicitly giving me the answer? Thank you.

I also have some other questions about python. I know its a commonly known language in computer security. What are the prerequisites of Black Hat Python? I am doing CCNA and I want to learn networking before I learn any hacking. The next thing I'm probably going to go for is a Linux Essentials cert and I'm wondering if security+ is necessary for reading Black Hat Python. I may do security+ anyways and then right afterwards do OSCP or OSWP cert course next summer. But I may decide after I complete my CCNA and Linux Essentials material to jump into Black Hat Python. I'm probably jumping ahead of myself, but is a CCNA and Linux Essentials a good set of prerequisites for Black Hat Python?

The other question is if I was looking for a wifi hacking course that used python. I found one but it was python programming separate from hacking. I think I may have seen a course here and there but does anyone know where I can find a good wifi hacking course that uses python? I may decide to do that instead of black hat python.

Thanks everyone. I know its a fuckload of topics to answer. If you don't want to answer all of them, you don't have to. I'm not a Nazi so I don't make strict rules like that and I don't have the authority to anyways (but honestly, I'm a nice guy so I don't want to). Thanks everyone.

yours truly,

fogstone

PS

This forum kicks ass!


* - soueuls - Sep 26, 2017

Quote:Again could someone tell me what I'm doing wrong without explicitly giving me the answer? Thank you.

list = [range(0,100)]

This is ultimately where the problem lies, range(0, 100) is what we call a generator. When you put something between brackets it creates a Python list.

list (the variable) is then a list containing one item of type generator. I will try to let you think about what happens in your for in loop.

Also, try to avoid naming your variable like "list" because this is already a Python builtin datastructure.

here some interesting read about when you should use a generator and when you should use a list.

🤝


* - fogstone - Sep 26, 2017

(Sep 26, 2017 01:56 PM)soueuls Wrote:  list = [range(0,100)]

This is ultimately where the problem lies, range(0, 100) is what we call a generator. When you put something between brackets it creates a Python list.

list (the variable) is then a list containing one item of type generator. I will try to let you think about what happens in your for in loop.

Also, try to avoid naming your variable like "list" because this is already a Python builtin datastructure.

here some interesting read about when you should use a generator and when you should use a list.

🤝

I figured it out. Here is the working code:

Code
fib = range(0,100)
for num in fib:
    if num == 0:
        i = 0
        print(str(i) + " ")
    elif num == 1:
        i = 1
        print(str(i) + " ")
    else:
        i = (fib[num - 1] + fib[num - 2])
        print(str(i) + " ")

Thanks so much.

You win at life.

Best,

fogstone


* - minimalist - Sep 26, 2017

I think you're misunderstanding the sequence. It should be dependent on the values of the past two terms, not their indexes.