Code works in Windows, but not on Mac
<p>So I'm new to programming, and recently I got an assignment to create a program that could convert binary to decimal and vice versa, and also decimal to hexadecimal and vice versa. The problem is, I'm not allowed to use available functions such as <code>int()</code> or <code>hex()</code> or <code>bin()</code>.</p>

<p>I might have found a way to do the conversion from decimal to binary, and I tried it out on my friends' laptop (which runs on Windows) and it worked. But when I tried to execute it in my own laptop (a MacBook), it won't run even though it's the exact same code. Here's the code:</p>

<pre><code>def dectobin(x):
    temporary_result = ""
    while x &gt; 0:
        result = str(x % 2) + temporary_result
        x = x // 2
    return result
</code></pre>

<p>Instead of the result being the binary number, it just returns the value of <code>x % 2</code>. Does anyone have any idea why it does that on a Mac and not on Windows? And if so, where did I go wrong? How do I fix it?</p>

<p>The value of <code>temporary_result</code> never changes, but remains <code>""</code>. Therefore the value of <code>result</code> will always be the <code>str(x % 2)</code> for the most-recently-processed value of <code>x</code>. Given that the loop terminates when <code>x</code> goes to zero, it's likely that the function always returns <code>1</code>.</p>

<p>Are you SURE this is the code you executed on Windows? You don't say how you know "it won't even run" but it's possible you are using different Python versions on the two different machines. </p>

python windows osx python-3.x