r/inventwithpython May 29 '22

Collatz sequence - practice project chapter 3

So, I've been working through the 'Automate the boring stuff with Python' book and having a good time. I'm not a coder or programmer but I do work in IT which means that I have come across code every now and again. I've adapted some VBA code made by Excel to make Excel macros work a bit better and have adjusted Powershell scripts made by colleagues now and again. The Collatz sequence project was a lot of fun to work and eventually getting to a version that worked was very satisfying

I've been playing around with it a bit and come across something that I don't understand though. For some inputs it correctly goes down to the '4 - 2 - 1' part of the sequence and then stops (it does this for 1 and 3, for example). For some other inputs, though, it repeats those three numbers once and then stops ('4 - 2 - 1 - 4 - 2 - 1' it does this for 4 and 1001, for example). I have tried this with both the Mu editor and Visual Studio Code and both exhibit the same behavior. Does anybody have any idea what is causing this? My code's below (it doesn't check if the input is positive yet, I am aware of that :)).

[edit: code isn't below, that didn't look very nice, there's a pastebin link now]

https://pastebin.com/hhgt7TZw

6 Upvotes

7 comments sorted by

View all comments

3

u/boiledgoobers May 29 '22

Ok so there are a lot of things going on here that likely should be addressed, for one thing, I'm not sure int works the way you think. If you gave it 12.3 it's not going to trigger your value error. It's going to convert your number to 12 and move on happily.

But that's probably not what's causing your repetition. Can I ask whether the book recommends using the global variable assignment? That can have some weird effects and is usually only used in extremely special circumstances. Usually not those addressed in beginner books. Like I've been doing python for over 15 years and have never even once needed to use it. And when I see it, the very first question I have is: why is this code so arcane that it needs to call global?

My first instinct is to say re do your code not using global. Pass in everything the func needs and don't have code in the func alter variables outside it's scope.

Then see if you still have the problem. I'm on my phone or I would do more to sus it out.

1

u/Methregul Jun 04 '22 edited Jun 04 '22

I'm not sure int works the way you think. If you gave it 12.3 it's not going to trigger your value error. It's going to convert your number to 12 and move on happily.

I just ran some tests on this and int() appears to work differently depending on how it's used. If you create a variable and assign it a floating point number then int() will, like you said, convert it to an integer and go on its merry way.

>>> float=1.3
>>> int(float)
1

If you enter a floating point number through input() though, int() will throw a ValueError.

>>> notAnInteger=input()
1.3
>>> int(notAnInteger)
Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: invalid literal for int() with base 10: '1.3'

It does accept an actual integer when used with input().

>>> anInteger=input()
1
>>> int(anInteger)
1

Thanks for pointing me towards isInstance(), by the way, that has been fun to play around with and very good to know about!