0
num = 0
for i in range(5):
    ask = int(input())
    if abs(ask) > 0:
        num = ask

print(num)

When I run the code, it lets me input the first string. But once I enter the second string, the program crashes and says "pythonProject\main.py", line 3, in ask = int(input()) ValueError: invalid literal for int() with base 10: ''"

What's going on?

My input: I just type 1, press enter, type 2, press enter and then it crashes. I am sure it is not an error where I click enter too quickly, or I accidentally type in an empty string because I've ran the code multiple times.

What have I tried so far?

  • Creating a new project and pasting the code -> didn't work
  • Asking my friend to copy the code onto his PyCharm and run it -> worked fine on his computer
  • 'Edit configurations', uncheck 'emulate code in output console' -> didn't work, it was already unchecked
  • Checked that I was running the correct file in the project -> didn't work, I was running the right file

EDIT:

  • FIXED, just needed to check 'Emulate code in output console' rather than uncheck it. Not sure why this works though, or how I can keep it checked for all future projects - rather than having to manually check it every time.
namdosan
  • 35
  • 3
  • Did you press too fast or too often? Like it complains that "" (aka an empty string) is not a number, which is absolutely correct. So the question is why is that string empty. Also you could give a message to the user upon that input which might be helpful – haxor789 Jun 10 '22 at 12:14
  • What was your input? – vmemmap Jun 10 '22 at 12:15
  • Maybe try this? https://stackoverflow.com/a/43621321/10513287 – ivvija Jun 10 '22 at 12:24
  • @haxor789 No, that isn't the issue. I just typed '1', enter, '2', enter and then it broke. Ran it many times to no avail. – namdosan Jun 10 '22 at 12:39
  • @Roi '1', enter, '2', enter (code crashes) – namdosan Jun 10 '22 at 12:39
  • @ivvija 'emulate terminal in output console' is already unchecked. – namdosan Jun 10 '22 at 12:41
  • @namdosan It works fine for me, it might be sth with your env rather than the code. – vmemmap Jun 10 '22 at 12:42
  • @Roi yeah, i know it's not an issue with the code. It's an issue with PyCharm, but I don't know how to fix this issue. – namdosan Jun 10 '22 at 12:45
  • 1
    It don't seems to be caused by PyCharm, when PyCharm run a python script, it simply execute `path/to/python3 path/to/script.py`, did you tried to run `python3 script.py` in your terminal manually ? – Marius ROBERT Jun 10 '22 at 12:51
  • "emulate terminal in output console" should be checked – Pavel Shishmarev Jun 10 '22 at 14:17
  • @PavelShishmarev It worked! How do I keep it checked by default? I opened a new project file but it was unchecked once again. And why does this even work in the first place? – namdosan Jun 11 '22 at 00:12
  • @S3DEV ah yes, i was trying to figure out how to approve Pavel's comment as the solution - didn't realise that I could write a solution myself. Thanks! – namdosan Jun 11 '22 at 09:32
  • @S3DEV yeah i know, it says i need to wait 9 more hours until I can though – namdosan Jun 12 '22 at 02:56

3 Answers3

2

FIXED, just needed to check 'Emulate code in output console' rather than uncheck it. Not sure why this works though, or how I can keep it checked for all future projects - rather than having to manually check it every time.

namdosan
  • 35
  • 3
0

The problem is with the input, as you are either pressing enter without any input (empty string) or you are entering a float value. This thread might help you in this case. The code is working fine when I input an integer and gives the same error when entered empty string or a float value.

Asadullah Naeem
  • 347
  • 2
  • 11
  • No I'm not - I'm inputting the string '3' and pressing enter. I'm confident I'm not inputting an empty string. – namdosan Jun 11 '22 at 00:09
0

To get it work you need to check "Emulate code in output console". I've answered in the comments section and I'm glad it worked out, here is an explanation:

You need to know a concept of "terminal emulator" to understand why and how this works. When a program is ran (at least on UNIX-like operating systems), it has three I/O streams: stdin, stdout and stderr. The stdin is used to input data, and two others are for output.

Input or output stream is just a buffer used to communicate with the program back and forth. Once something is written to the buffer, it can be read from there. If the buffer is empty, an attempt to read from there will cause stall until the buffer has something in it. More about stdio: https://en.wikipedia.org/wiki/Standard_streams

When the program is ran through the terminal emulator, I/O streams are connected to this emulator, so whatever you type in the terminal window is written to your stdin by default. Whatever your program writes to the stdout and stderr is displayed on the screen. (However, this behavior may be changed using pipes, so you can pass data from some file to the stdin and also you can redirect the output to the file)

Here is the history behind terminal emulators, to understand, why is it implemented this way: https://en.wikipedia.org/wiki/Terminal_emulator#Computer_terminals

For example, you have a simple program:

s = input('Enter string: ')
print(f'stdout: {s}')

If you run it from the terminal and type "TEST":

$ python3 test.py
TEST
stdout: TEST

But you also can, for example, pass data directly to stdin, and redirect output to the file:

$ echo "ABCDEF" | python3 test.py > OUTPUT.txt

there will be no text in the terminal, but OUPUT.txt will appear. It will contain:

stdout: ABCDEF

Now, about PyCharm: By default, when it runs your script, it does not automatically emulate terminal in the output window. It simply does not send anything to the stdin and it won't react to pressed keys. When your program gets to the line with input(), it starts to read the stdin stream until it gets \n character from the stream (indicating that user has pressed Return key). As nothing gets sent to the stream, it will wait infinitely.

Useful tip: for testing, instead of just typing something into the terminal every time, you can also check "Redirect input from:" and choose an input file.

Pavel Shishmarev
  • 1,335
  • 9
  • 24