1

I am an absolute beginner in python and i have an error in my code. am doing it using python terminal. I know it may be simple but as am new i cant figure it out. My code is :

num = input("Enter a number: ") ;\
... mod = num % 2 ;\
... if mod > 0:

and the error is :

File "<stdin>", line 3
    if mod > 0:
     ^
2
  • line continuations are rarely useful in python... as well as semicolons. Check the answer below Commented Aug 22, 2017 at 7:03
  • then what should i do Commented Aug 22, 2017 at 7:04

2 Answers 2

2

That is because you are required to send an input to the input function on the next line. Also, with Python 3 input, you need to convert the input to numeric data type. Do something like this.

num = int(input("Enter a number: ")) 
5
mod = num % 2
if mod > 0:
    print(mod)
Sign up to request clarification or add additional context in comments.

6 Comments

i didnt really understand
When you take an input from the user, it is stored as a string even if you have passed an integer as input. So you need to convert it to numeric data type either using int or float function. Secondly, since you are asking for an input, you need to provide that input to python to further the program
if you don't understand, just copy/paste the code of the answer (it works), then read a python tutorial. This isn't C. Also, you seem to work exclusively from python console. I recommend that you work from a python script instead (using an IDE like Pycharm, Pyscripter...)
@Jean-FrançoisFabre isn't it better to work on terminal than IDE's. what is your opinion
because you can write bigger scripts and debug them, instead of retyping everything each time. don't see python as a command line language. it's a full-fledged language.
|
1

When you take input in Python, it is stored as a string. This means this:

mod = num % 2

Is like doing this:

mod = "5" % 2

Which you can't do. You should add a line before the modulo operation, converting the string to an integer. Also, you have to give input to the console after the input() function. I'm not able to see if you have written any more code, but if you haven't, you also need to add the code that would be executed under the if statement, or you'll get a SyntaxError.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.