Input() Function Errors (help)

Problem with input() function in python 3.10.0
When i try to run the entire script shown below the input() function doesn’t prompt. Instead i get a list of different errors. I want to know why i can’t run the entire script without getting errors? If i run the input() function alone, enter a integer value, and run the rest of the script, the result is fine. But i want to run the entire thing at once.

Script
num = int(input('Enter value: '))

if num < 0:

print('The integer is negative')

elif num == 0:

print('The integer is zero')

else:

print('The integer is positive')

Terminal

num = int(input('Enter value: '))
Enter value: if num < 0:
Traceback (most recent call last):
File “”, line 1, in
ValueError: invalid literal for int() with base 10: ‘if num < 0:’

print('The integer is negative')

File “”, line 1
print(‘The integer is negative’)
IndentationError: unexpected indent

elif num == 0:
File “”, line 1
elif num == 0:
^^^^
SyntaxError: invalid syntax

print('The integer is zero')

File “”, line 1
print(‘The integer is zero’)
IndentationError: unexpected indent

else:
File “”, line 1
else:
^^^^
SyntaxError: invalid syntax

print('The integer is positive')

File “”, line 1
print(‘The integer is positive’)
IndentationError: unexpected indent

The error you are getting is “IndentationError: unexpected indent”. That means that it’s not the content of the lines, but the formatting of your code that is at issue.

Unfortunately, the raw paste doesn’t show the formatting. Please use three backquotes before and after your code to maintain the formatting so we can see the indentation.

How are you running this code? Are you saving it to a file and then running “python [filename]”? It looks like you might be trying to paste the code into a python interpreter. That can work for some programs, but not if it’s prompting for input. It asks for input and reads the next pasted line as the input.

Comments inline below…

Problem with input() function in python 3.10.0
When i try to run the entire script shown below the input() function doesn’t prompt. Instead i get a list of different errors. I want to know why i can’t run the entire script without getting errors? If i run the input() function alone, enter a integer value, and run the rest of the script, the result is fine. But i want to run the entire thing at once.

Script
num = int(input('Enter value: '))
if num < 0:
print(‘The integer is negative’)
elif num == 0:
print(‘The integer is zero’)
else:
print(‘The integer is positive’)

Terminal

num = int(input('Enter value: '))
Enter value: if num < 0:

It seems to me that you are cut/pasting this directly to the interactive
interpreter. Because of that, you have pasted the if-statement “if num <
0:” as the response to the input prompt!

Traceback (most recent call last):
File “”, line 1, in
ValueError: invalid literal for int() with base 10: ‘if num < 0:’

And as a result of that, it has tried to convert “if num < 0:” into an
int, which goes badly. The string itself is even in the error message
above, at the end of the last line above.

If you’re going to do this you need to paste one line at a time, and
enter a number after you have pasted the “num = int(input('Enter value:
'))” line.

print('The integer is negative')

File “”, line 1
print(‘The integer is negative’)
IndentationError: unexpected indent

Because the if-statement was consumed by the input(), it is not part of
the programme code. Because of that, the print() is not inside an
if-statement, and therefore is not expected to be indented.

The rest of the errors are the same kind of thing.

Pasting interactive code directly to the ">>> " prompt is generally
tedious and error prone, as you see above.

You’d be better off putting this code into a file on its own, eg
“my_script.py”. Then you can run it from a command prompt like this:

python3 my_script.py

This has the added benefit that when there are problems you can just
edit the file and rerun it.

Cheers,
Cameron Simpson cs@cskk.id.au

Hi, thanks for the reply. Sorry about the poor formatting before. I haven’t used this forum before and was kinda confused how to set up a good question post. I have listed my code and added text from terminal. I tried running the code in 3 different way and added the terminal text below for all 3. Not sure exactly sure what differences they have. I still want to use shift+enter to run the code and get quick results but haven’t been able to. I got the result i wanted by debugging the python file in VS-Code. But i don’t feel like that’s the “correct” approach, for no good reason other than intuition. I am using Visual Studio Code to run the code. I saved the file as you can see in one of the listed terminal below. I tried moving it to different folder but that did not solve anything.

Code

1. num = int(input('Enter value: '))

2. if num < 0:

3.     print('The integer is negative')

4. elif num > 0:

5.     print('The integer is positive')

6. else:

7.     print('The integer is zero')

Terminal results when using Run Selection/Line in Interactive Window (shift+enter) (not code)

>>> num = int(input('Enter value: '))
Enter value: if num < 0:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'if num < 0:'
>>>     print('The integer is negative')
  File "<stdin>", line 1
    print('The integer is negative')    
IndentationError: unexpected indent     
>>> elif num == 0:
  File "<stdin>", line 1
    elif num == 0:
    ^^^^
SyntaxError: invalid syntax
>>>     print('The integer is zero')
  File "<stdin>", line 1
    print('The integer is zero')    
IndentationError: unexpected indent 
>>> else:
  File "<stdin>", line 1
    else:
    ^^^^
SyntaxError: invalid syntax
>>>     print('The integer is positive')
  File "<stdin>", line 1
    print('The integer is positive')    
IndentationError: unexpected indent     
>>> 
>>> 

Terminal Results when using Run Python File (not code)

>>> & C:/Users/Emil/AppData/Local/Programs/Python/Python310/python.exe "c:/Users/Emil/OneDrive - Syddansk Universitet/University of Southern Denmark/Courses/T090036101 Programming with Python/Exercises/test.py"
  File "<stdin>", line 1
    & C:/Users/Emil/AppData/Local/Programs/Python/Python310/python.exe "c:/Users/Emil/OneDrive - Syddansk Universitet/University of Southern Denmark/Courses/T090036101 Programming with Python/Exercises/test.py"
    ^
SyntaxError: invalid syntax
>>> 

Terminal Reults when using Debug Python File (not code) (WORKS but not what i want)

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

Try the new cross-platform PowerShell https://aka.ms/pscore6

PS C:\Users\Emil\OneDrive - Syddansk Universitet\University of Southern Denmark\Courses\T090036101 Programming with Python\Exercises>  & 'C:\Users\Emil\AppData\Local\Programs\Python\Python310\python.exe' 'c:\Users\Emil\.vscode\extensions\ms-python.python-2021.11.1422169775\pythonFiles\lib\python\debugpy\launcher' '51166' '--' 'c:\Users\Emil\OneDrive - Syddansk Universitet\University of Southern Denmark\Courses\T090036101 Programming with Python\Exercises\test.py'
Enter value: 1
The integer is positive
PS C:\Users\Emil\OneDrive - Syddansk Universitet\University of Southern Denmark\Courses\T090036101 Programming with Python\Exercises> 

Hi Cameron.
Thanks for the reply. I really appreciate you thorough explanation and it cleared up things a lot for me. Please see my reply to"BowlOfRed" for a better explanation of my problem (if you need it). I think i understand what you are saying. I am using Visual Studio Code and i believe it should be possible to have the input() statement before the “if” statement without the “if” statement being consumed by the input prompt. I’m not really intereseted in using cmd everytime to check my code as it is not very practical. I’m very new to coding so maybe i’m wrong about that.

You say when running python from powershell it works, but it’s not what you want. Can you describe what you want to change about it? Generally you only use the interactive shell (the one with the >>> prompt) for testing or very short snippets. Otherwise you’d run it in a way similar to the powershell example you show.

This is the output from powershell (Too much useless info):

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

Try the new cross-platform PowerShell https://aka.ms/pscore6

PS C:\Users\Emil\OneDrive - Syddansk Universitet\University of Southern Denmark\Courses\T090036101 Programming with Python\Exercises>  & 'C:\Users\Emil\AppData\Local\Programs\Python\Python310\python.exe' 'c:\Users\Emil\.vscode\extensions\ms-python.python-2021.11.1422169775\pythonFiles\lib\python\debugpy\launcher' '51166' '--' 'c:\Users\Emil\OneDrive - Syddansk Universitet\University of Southern Denmark\Courses\T090036101 Programming with Python\Exercises\test.py'
Enter value: 1
The integer is positive
PS C:\Users\Emil\OneDrive - Syddansk Universitet\University of Southern Denmark\Courses\T090036101 Programming with Python\Exercises>

The only output i want and need is this:

Enter value: 1
The integer is positive

I can accept if this is the output as it is isn’t too descriptive:

>>> num = int(input('Enter value: '))
Enter value: 1
>>> if num < 0:
...     print('The integer is negative')
... elif num == 0:
...     print('The integer is zero')
... else:
...     print('The integer is positive')
...
The integer is positive

The Powershell has too much useless information:

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

Try the new cross-platform PowerShell https://aka.ms/pscore6

PS C:\Users\Emil\OneDrive - Syddansk Universitet\University of Southern Denmark\Courses\T090036101 Programming with Python\Exercises>  & 'C:\Users\Emil\AppData\Local\Programs\Python\Python310\python.exe' 'c:\Users\Emil\.vscode\extensions\ms-python.python-2021.11.1422169775\pythonFiles\lib\python\debugpy\launcher' '51166' '--' 'c:\Users\Emil\OneDrive - Syddansk Universitet\University of Southern Denmark\Courses\T090036101 Programming with Python\Exercises\test.py'
Enter value: 1
The integer is positive
PS C:\Users\Emil\OneDrive - Syddansk Universitet\University of Southern Denmark\Courses\T090036101 Programming with Python\Exercises> 

This is the output i want or something similar (the point is there’s only the information i need):

Enter value: 1
The integer is positive

I can accept this output as it isn’t too descriptive:

Enter value: 1
>>> if num < 0:
...     print('The integer is negative')
... elif num == 0:
...     print('The integer is zero')
... else:
...     print('The integer is positive')
...
The integer is positive
>>>

I guess i want to figure out why the code works with Powershell and not the python shell. Or whatever you call it.

It doesn’t work in the interactive shell because it’s running your code immediately. When it runs the code for input, it wants the input next, not the next line of code.

The verbosity of powershell (or a windows cmd shell) is due to that terminal, not python. But it’s how most people expect to run command-line applications. You have a shell prompt, you run your program. On most windows machines python is runnable just from typing py, rather than the entire path.

It doesn’t work in the interactive shell because it’s running your code
immediately. When it runs the code for input, it wants the input
next, not the next line of code.

To elaborate, it is likely that PowerShell gets your cut/paste it loads
the whole pasted text as the code to run, hence the nicer behaviour.

At the interactive (">>> ") prompt it reads code line by line, running
as soon as it has a complete statement. Which means the input() runs
immediately and reads the next line of code as input.

The verbosity of powershell (or a windows cmd shell) is due to that
terminal, not python. But it’s how most people expect to run
command-line applications. You have a shell prompt, you run your
program. On most windows machines python is runnable just from typing
py, rather than the entire path.

Most of us not using an IDE for everything run things from the command
line. My personal setup is a pair of terminals, an editor in the one on
the left and a command prompt on the right. Save file from editor, run
it from the command prompt.

Once you’ve typed “py your_file.py” once, running it again is probably
just an UpArrow, Enter away for next time.

Of course you should use whatever you find most convenient, but the
interctive prompt is clearly not doing it for you with your interactive
programme (because it uses input()). But it is fiddly for other things
too, particularly longer chunks of code. Have it in a file is a huge
win, you just need to find the most convenient process to use it that
works for you.

Besides, once your programme is working well you will naturally want to
keep it around for reuse, won’t you? That means you want it in a file
anyway :slight_smile:

Cheers,
Cameron Simpson cs@cskk.id.au