god of llamas
god of llamas

Reputation: 109

Python IDLE with Python 3.5.2 "crashing"

Okay, so let me just say beforehand: I am new to Python. I was just experimenting with IDLE and then I had this weird "crash". I put "crash" inside speech marks because I'm not sure if it qualifies as a crash, as rather than the program just crashing the way a normal program would in Windows, it still runs, but whenever I press enter and try and get it to accept new text it doesn't do anything. E.g. if you try and type "print('a')" and then hit enter it just goes to the next line (and doesn't print 'a'). I tried to make a simple function which converted an integer to a string where each character in the string was either a '1' or a '0', forming the binary number representing said (unsigned) integer.

>>> def int_to_str(int_in):
        str_out=''
        bit_val=1<<int_in.bit_length()
        while(int_in>0):
            if(int_in>bit_val):
                str_out+='1'
                int_in-=bit_val
            else:
                str_out+='0'
            bit_val>>=1
        return str_out

>>> print('a')
print('c')

Basically, it becomes completely unresponsive to my input, and allows me to edit/change "print('a')" even though I shouldn't be able to if it had actually "accepted" my input. Why is this? What have I done wrong/messed up?

Also, I made sure it isn't something else I was previously messing around with by closing the shell and opening it again and only putting in said code for the "int_to_string" function, and I haven't changed any settings or imported any modules before hand or anything like that (in case it matters).

EDIT: I tried reinstalling, and that helped a bit in that I can now do other stuff fine, but the moment I try to use the "str_to_int()" function, it has this same weird behaviour of not accepting/interpreting any more user input.

Upvotes: 0

Views: 452

Answers (1)

wwii
wwii

Reputation: 23753

Your while loop never terminates, you need to re-arrange your logic. Printing variables can be an effective debugging tool - like this:

>>> def int_to_str(int_in):
        str_out=''
        bit_val=1<<int_in.bit_length()
        while(int_in>0):
            print(int_in, bit_val)
            if(int_in>bit_val):
                str_out+='1'
                int_in-=bit_val
            else:
                str_out+='0'
            bit_val>>=1
        return str_out

If your program seems to be going on too long you can stop it with ctrl-c.

Upvotes: 1

Related Questions