this post was submitted on 07 Jun 2023
11 points (100.0% liked)

Programming

3347 readers
1 users here now

All things programming and coding related. Subcommunity of Technology.


This community's icon was made by Aaron Schneider, under the CC-BY-NC-SA 4.0 license.

founded 1 year ago
MODERATORS
 

you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 2 points 1 year ago (2 children)

That's actually pretty good for a beginner.

Some tips (take with a grain of salt since some are just personal preference):

Make collatz take a number, and parse the number outside the loop, which also avoids needing to repeat the conversion to int. It also means that you can remove the try-catch from inside collatz, since the only place a ValueError is expected is the conversion to int.

def collatz(number):
    ...

number = None # Declare number to give it the correct scope
while True:
    try:
        number = int(input('Type in a positive integer: '))
        break
    except ValueError:
        print("Input could not be parsed")

print(collatz(number))

Avoid commenting what your code does, and instead focus on getting across the intent, like how you commented that int(number) % 2 == 0 is a check for even numbers. It is fine to leave out comments for pieces of code that are truly self explanatory.

The elif condition can be replaced with just else, since it will only be run if the if condition is false.

Format with Black, it makes things look nicer without having to deal with manual formatting.

[–] [email protected] 2 points 1 year ago (1 children)

I appreciate the pointers, but I'm definitely not a beginner anymore. This script is like 4 years old lol.

[–] [email protected] 3 points 1 year ago

Oops. Hopefully it helps others though!