0

I am trying to get user input as an integer in my program with the following code:

while True:
    x = input("Enter the integer: ")
    try:
        num = int(x)
    except ValueError:
        print("input should be an integer")
        continue
    break

If I run it repeats "Enter the integer" "Enter the integer" "Enter the integer" "Enter the integer" and so on even though I am not typing anything into the keyboard

I have already closed the serial port through pySerial so I don't think I am getting input from an external device.

Is there a problem in the way I have written this code?

I am using the terminal JetBrains PyCharm Community Edition IDE with PySerial 2.7, Python 3.4 on Windows XP

ignQ1744a
  • 31
  • 2
  • 4
    no one else will be able to repeat this with the code given ... try literally copy/paste this into pycharm or even just python terminal and it will work how you expect... is your enter key stuck on your kyboard perhaps? – Joran Beasley Jul 23 '15 at 18:57
  • A long shot: maybe `input` is not throwing an `EOFError` at end of input, but is instead just returning an empty string. `int('')` thus fails with `ValueError` and the loop goes on forever. Try adding an `if not x: break`, and if that doesn't work, `print(x)` in the `ValueError` handler to see what you're getting. – Claudiu Jul 23 '15 at 19:15
  • Works perfectly under Windows 7 with Python 3.4.1. Can't really see anything wrong with this code. Have you tried running it on a different machine (or perhaps OS)? Just to isolate and narrow down the problem you are having. Unless there's something funky about XP, I suspect it's hardware related. – Levon Jul 23 '15 at 19:16
  • http://stackoverflow.com/questions/983354/how-do-i-make-python-to-wait-for-a-pressed-key check this out – FirebladeDan Jul 23 '15 at 19:35

0 Answers0