0

I am new to Python and have tried some problems to code in this but got this same error everytime. So I tried this simple code and again got the same error.

t = raw_input()
for i in range(int(t)):
    print i

      1 t = raw_input()
----> 2 for i in range(int(t)):
      3     print i

ValueError: invalid literal for int() with base 10: ''

I don't know what's wrong here. Please help. i am using Python 2.7

Anuj
  • 39
  • 2
  • 6
  • 3
    You need to give a valid integer input. – Anand S Kumar Aug 23 '15 at 15:23
  • 2
    An empty string is not a valid int. – Maroun Aug 23 '15 at 15:23
  • Don't modify t in a loop on t. – Elliott Frisch Aug 23 '15 at 15:24
  • Yes I know it's because type of an empty string cannot be modified. But how should I write it ? – Anuj Aug 23 '15 at 15:26
  • 1
    @Anuj You just said "I don't know what's wrong here." in the question? Anyways, what do you want to do if the string is empty? You're not inputting a number – Markus Meskanen Aug 23 '15 at 15:28
  • @Markus You mean I should use try and except for that. Because string will be empty until I run the program and input some integer. So it will always remain empty there. – Anuj Aug 23 '15 at 15:34
  • 1
    @Anuj I don't think I'm quite following. Your program will never go past the line `t = raw_input()` until you press enter on your keyboard. So if the string is empty, that's because you aren't inputting anything, you're just hitting enter. If you want to make sure the string is a number, use `try` and `except` like you mentioned. – Markus Meskanen Aug 23 '15 at 15:37
  • 1
    It might be less confusing if you give `raw_input()` a prompt string. Eg, change it to: `t = raw_input('Enter a number: ')`. Then when you run your program it will print the message `Enter a number: ` and wait for you to type something & hit the Enter key. – PM 2Ring Aug 23 '15 at 15:44
  • Yes, I got that. I was 'enter'ing all the time not realising I had to give input first. – Anuj Aug 23 '15 at 16:10

1 Answers1

5

When the interpreter executes:

t = raw_input()

it expects an entry back from you before you hit enter, because you are hitting enter and giving it back an empty input, which explains your error,

ValueError: invalid literal for int() with base 10: '' (These empty quotes means you provided nothing)

so I suggest this modification so you're not confused:

t = raw_input("Please provide an Integer then hit enter: ")
for i in range(int(t)):
    print i
YOBA
  • 2,759
  • 1
  • 14
  • 29
  • 1
    That was as simple as that. I always hit enter and never thought that it was asking for input. Thanks a lot. – Anuj Aug 23 '15 at 16:09