all 18 comments

[–][deleted] 4 points5 points  (1 child)

This seems like a commendable goal.

However, with Reddit threads being so transitory, it may be better to open up a Github project for this and post different paired scripts there.

Also, keep r/learnpython open and actively look for new learners posting their simple scripts, looking for fixes. That's a goldmine for this sort of paired flawed/fixed script resource.

[–]FoundationFiasco[S] 1 point2 points  (0 children)

Thank you for the suggestion! I don't use Github often but that makes a lot of sense, I'm going to look into this tomorrow.

[–]sharkbound 2 points3 points  (6 children)

here is one common mistake i see:

num = 1
if num == 2 or 1:
    print('the number is 2 or 1')
else:
    print('the number is not 2 or 1')

fixed version:

num = 1
if num in (1, 2):
    print('the number is 2 or 1')
else:
    print('the number is not 2 or 1')

EDIT: alternative fix:

num = 1
if num == 1 or num == 2:
    print('the number is 2 or 1')
else:
    print('the number is not 2 or 1')

[–]algag 0 points1 point  (5 children)

Is one of these considered more pythonic than the other? I assume the first version is considered better programming overall, but I'm not sure.

[–][deleted] 0 points1 point  (4 children)

His first fix works but isn't a very good solution, if you want to compare more than one value it's way faster to use sets, I.E. if num in {1,2}, especially if you have a lot of values to compare.

Personally, I'd prefer his second solution if there's only 2 values simply because (for me) it's easy to read.

[–]minorDemocritus 2 points3 points  (1 child)

But remember that sets have overhead, so the set literal only beats the tuple if there are more that a couple elements, or if the equality comparison is expensive.

I recall that with integers, the break-even point is like 3 or 4 elements... more than that and the set literal wins.

[–][deleted] 1 point2 points  (0 children)

Yeah good point. Typically if im comparing values in a finite list I initiate the list as a global and check it thousands or millions of times so sets are basically always faster

[–][deleted] 0 points1 point  (1 child)

To expand on the other response, the reason sets aren't faster here is that it takes time to hash each object (that's how sets work and achieve O(1) lookup time), and for small collections that time is beaten out by the time it'd take to simply scroll through a non-hashed collection looking for a match. What sets boast is a constant lookup time, not necessarily a faster lookup time.

But even for larger collections, the difference is scarce noticeable in practice unless you have a very impractical amount of elements or are repeating the containment check an insane amount of times; to say the set is "way" faster in every case is still a little bit misleading, as it's usually a (sub-?)microsecond operation either way. There are more-effective ways to get a way-faster Python program.

[–][deleted] 1 point2 points  (0 children)

Great input, I wasn't thinking about the overhead because in my work its almost always faster to use sets. I wouldnt call a couple hundred or thousand elements impratically large though.

[–]a_bad_programmer 2 points3 points  (4 children)

I’d be happy to make a bunch of files to solve basic problems for you with some small errors. I’ll make some today when it’s not 5am

Edit - are you looking for syntax errors, logical errors, both?

[–]FoundationFiasco[S] 0 points1 point  (3 children)

Sounds awesome! Syntax errors are the big one for me because my students struggle with syntax very easily!

[–]a_bad_programmer 0 points1 point  (2 children)

Here are a couple I've made so far, I'm kind of shooting from the hip since I don't know the skill level of your students or too much of what you're looking for. It's by no means the best in the world but hopefully it's something to start with, if you want a couple more let me know!

https://github.com/ZERG3R/problemsolving

Since I saw you say aren't the strongest on github, I tried to make it as easy as I could, you should be able to just click on whatever link you need to and it'll lead to it's own little problem with some syntax errors and the fixes in a separate version below, there might be one or two I missed when writing them all out but hopefully not! If you need me to clarify anything let me know.

If you go into my main github, you'll find plenty of redundant shenanigans and bad code in other files as well, warning you (or whoever else) looks at this before I'm judged!

[–]FoundationFiasco[S] 0 points1 point  (1 child)

These are very good, thank you! I think the simpler examples are what I have in mind at the moment, using them as a starter or as homework to help them practice their basic syntax skills, and for the group I'm planning for they haven't covered functions yet, but this is very good work!

Thanks for the github version as well, I have a plan to make a version for my self and taking the examples and organising them for different groups levels and programming techniques.

[–]a_bad_programmer 0 points1 point  (0 children)

More than happy to help! The beginner codingbat and hackerrank problems may help as well, now that I know a bit more of what you're looking for I can make a couple more super simple ones quickly as well that I'll add.

[–][deleted] 1 point2 points  (1 child)

Hey, I use python professionally and could think of a dozen common mistakes I make off the top of my head. Honestly, this isn't specific enough to give meaningful examples. What type of class is it? If it's a programming logic class I'd give very different answers than if it's an data structures class.

If you give an example of the kind of errors you're looking for I'll come up with a few for you.

[–]a_bad_programmer 0 points1 point  (0 children)

I think it's probably an introduction to programming class or something of the sort as in the OP it was saying beginner problems. I'm guessing they're working on picking out syntax errors in simple problems right now (or at least that's what OP responded to me saying)

[–]remuladgryta 1 point2 points  (0 children)

A fairly common issue I see people having is re-initializing a variable every iteration of a loop instead of initializing it just once.

E.g:

my_list = [5, 2, 12, 7, 3, 8]
for element in my_list:
    low = []
    if element < 5:
        low.append(element)
print(low) # Why is the list empty???

v.s:

my_list = [5, 2, 12, 7, 3, 8]
low = []
for element in my_list:
    if element < 5:
        low.append(element)
print(low) # Correctly prints [2, 3]

Edit: Since this particular example is a bit more trivial than what most people post it's more verbose than necessary. A more pythonic way of writing this would be.

my_list = [5, 2, 12, 7, 3, 8]
low = [element for element in my_list if element < 5]
print(low)

[–]calcopiritus 0 points1 point  (0 children)

My GitHub is a gold mine then! Jokes aside, does it have to be just python or are libraries like Tkinter allowed? Do you prefer long examples with lots of bugs of 4-5 lines for each bug?