Feb-20-2018, 09:05 AM
I'll start by listing a few problems I see with the code:
All that said, what's really important is you wrote some code that does what you want.
That, along with reading others people's code, is the best way to improve.
- Initializing all of those variables at the start of your code is not needed
- The
global
statement is very rarely a good idea; pass arguments to functions and return values instead of modifying globals and appending to lists, and if you need state, use classes
- You should never write loops over the indices of a list if you actually want elements. Instead of:
for i in range (0,100): if dblist[i] == 1:
use
for number in dblist: if number == 1:
You can take a look at this talk to learn more about python's loops.
- A function should only do one thing. Your
random100()
function creates a list of numbers, finds streaks in it, and returns the longest ones. Splitting it into a few simpler functions would make the code more readable and easier to test and maintain.
All that said, what's really important is you wrote some code that does what you want.
That, along with reading others people's code, is the best way to improve.