-2

Please help me why on executing the below program an error coming on m variable

x=int(input("Enter first number"))
y=int(input("Enter second number"))

def multiplication():
 m=x*y
print("Multiplication result"m)
Vaibhav Agrawal
  • 27
  • 1
  • 1
  • 6

4 Answers4

3

In Python 2, you should accept user inputs with raw_input(): Check this.

x=int(raw_input("Enter first number"))
y=int(raw_input("Enter second number"))

Please follow a proper indentation plan with python:

Also, you did not accept the variables while defining your function, and learn how to use print:

def multiplication(x, y):
    m = x * y
    print "Multiplication result: %d" % m

Finally, to call this function, use:

multiplication(x, y)
Community
  • 1
  • 1
Ajeet Shah
  • 18,551
  • 8
  • 57
  • 87
1
x=int(raw_input("enter first number"))
os=raw_input("Enter the sign of what you wanna do +,-,/,*")
y=int(raw_input("enter second number"))
Vaibhav Agrawal
  • 27
  • 1
  • 1
  • 6
  • 1
    You might consider editing your answer and add at least one line about what your program does. Also, you can write what your problem was in your question and how you solved your problem. You can also accept your own answer. – Ajeet Shah Jun 18 '16 at 14:48
  • Okay I am edited and will remember this forever Thankyou! – Vaibhav Agrawal Jun 18 '16 at 15:27
  • Better, you do rollback, earlier one was better. – Ajeet Shah Jun 18 '16 at 15:33
  • Cam you please tell me how to learn python ? Now I am using youtube as well as python.org – Vaibhav Agrawal Jun 18 '16 at 16:24
  • Check these: [1](http://stackoverflow.com/a/3226704/2873538), [2](http://www.learnpython.org/), [3](https://www.codecademy.com/learn/python), [4](http://learnpythonthehardway.org/book/), [5](https://developers.google.com/edu/python/) and search on Internet for more. Follow the tutorial which you like and find easy to learn. Or if you like books: check [this](http://www.informit.com/articles/article.aspx?p=1849069) post. Important: check [Python2 or 3](https://wiki.python.org/moin/Python2orPython3) – Ajeet Shah Jun 18 '16 at 16:35
1

You can also do like this if you want to keep it in functions.

def input_function():
    x = int(raw_input("Enter first number"))
    y = int(raw_input("Enter second number"))
    return x,y


def multiplication():
    x,y = input_function()
    m = x * y
    print "Multiplication result", m

multiplication()

Or like this, in one function. But it doesn't look so pretty.

def multiplication(x,y):
    m = x * y
    print "Multiplication result",m


multiplication(int(raw_input('Enter first number')),int(raw_input('Enter second number')))
CasperTN
  • 441
  • 1
  • 5
  • 14
0
def r():
    v = int(input("voltage: "))
    i = int(input("current: "))
    resistance = v*i
    return(resistance,'ohms')
  • While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Dharman Jul 22 '20 at 12:19