1

I was writing a python code in VS Code and somehow it's not detecting the input() function like it should. Suppose, the code is as simple as

def main():    
    x= int ( input() )
    print(x)

if __name__ == "__main__":
    main()

even then, for some reason it is throwing error and I cannot figure out why. The error being- enter image description here

P.S. 1)I am using Python 3.10 2) I tried removing the int() and it still doesn't work.

4
  • It's because the input value cannot be converted to int Commented Mar 12, 2022 at 9:30
  • what are you giving as the input? Commented Mar 12, 2022 at 9:35
  • Hello @ramzeek , the prompt for the input did not come. Before that it is throwing the error. Commented Mar 12, 2022 at 9:39
  • I ran your code, and it works. If you enter a number, it prints the number but if you enter a string of characters, you get a ValueError. There is no prompt for input because you have input() but the shell is waiting for input. If you write something like input('need input:> ') then you should see the input prompt. Commented Mar 12, 2022 at 9:44

3 Answers 3

1

The traceback shows you where to look. It's actually the int function throwing a ValueError. It looks as if you're feeding it a filepath whereas it it's expecting a number.

You could add a check to repeat the input if incorrect like so:

user_input = None
while not user_input:
    raw_input = input("Put in a number: ")    
    try:
        user_input = int(raw_input)
    except ValueError:
        continue

print(f"Number is: {user_input}")
Sign up to request clarification or add additional context in comments.

Comments

0

it's working!!!

see my example that says why you don't understand this:

>>> x1 = input('enter a number: ')
enter a number: 10
>>> x1
'10'
>>> x2 = int(x1)
>>> x2
10
>>> x1 = input()  # no text
100
>>> # it takes
>>> x1
'100'
>>> # but how you try?
>>> x1 = input()
NOT-NUMBER OR EMPTY-TEXT
>>> x2 = int(x1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'IS-NOT-NUMBER OR EMPTY TEXT'
>>>

I thing this is enough.

2 Comments

Thanks @Delta , now I see that input is working for me too. However, when I try to type cast the input into int, the problem comes.
@KaraageAndKamehameha So now two questions arise: what exactly is the problem? How is it solved? If my guess is correct, this is not the place for your question. However, I hope you succeed in any way you can
0

The int() func is try to convert your string to Integer so it should be just numbers. It seems you are giving number and characters as an input so it raise the Value Error. if you want you can check if it is just numbers or not

x = input()
if x.isdigit():
   x = int(x)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.