0

I am trying to write a basic algorithm for addition and it is giving me an error that says: Traceback (most recent call last): Traceback (most recent call last): File "solution.py", line 6, in print C NameError: name 'C' is not definede I erased the first print statement and it fixed this issue but now it says 'a'is not defined

def solveMeFirst(a,b):

    a = int(raw_input('A='))
    b = int(raw_input('B='))
    C = a+b
    print C
res = solveMeFirst(num1,num2)
print res
4
  • 2
    C looks adequately defined to me, at least in this function. Commented Jan 14, 2015 at 5:20
  • The original code puts print C outside the function (see source), which causes the error C is not defined; the edit accidentally indented it. Commented Jan 14, 2015 at 5:25
  • 1
    So this is a case of both a typo and the editor doing an overzealous job, eh? Commented Jan 14, 2015 at 5:26
  • 3
    So now comes the burning question: now that the OP has edited the question once again, is this the correct indentation? This is very important. If it is, then there's nothing syntactically wrong with it unlike what you indicate in your question. Commented Jan 14, 2015 at 5:29

3 Answers 3

2

I get NameError: name 'num1' is not defined

Which makes sense since num1 and num2 aren't defined anywhere.

num1 = 1 
num2 = 2 
def solveMeFirst(a,b):
   a = int(raw_input('A='))
   b = int(raw_input('B='))
   C = a+b
   print C
res = solveMeFirst(num1,num2)
print res

Works.

res is None because you don't return any value from solveMeFirst.

If you try print C outside the function solveMeFirst, it will be undefined since it is only defined inside the function.

I think what you are trying to do is the following:

num1 = int(raw_input('A='))
num2 = int(raw_input('B=')) 
def solveMeFirst(a,b):
   return a+b
res = solveMeFirst(num1,num2)
print res
Sign up to request clarification or add additional context in comments.

1 Comment

I read the answer entirely too fast at first, but this is probably the cleanest solution out there.
0
  1. Get num1 and num2 from User by raw_input method.
  2. Call your function.
  3. Return Addition value from function.
  4. Get value in res and print.

code:-

def solveMeFirst(a,b):
    C = a+b
    return C

num1 = int(raw_input('A='))
num2 = int(raw_input('B='))
res = solveMeFirst(num1,num2)
print res

Output:

$ python test.py 
A=2
B=2
4

3 Comments

This doesn't explain why he thinks C isn't defined.
yes correct, but post was missing code details. I just provided suggestion so he can understand what wrong in the code.
First, the fact that he wasn't returning anything (instead he was printing) wasn't what he was saying was wrong. Second, if the question doesn't have details, why bother answering it until you have the whole picture?
0
def solveMeFirst():

    a = int(raw_input('A='))
    b = int(raw_input('B='))
    C = a+b
    return C                    # you need to return value

res = solveMeFirst()   #no need of argument
print res

output:

A=10
B=34
44

4 Comments

Even without the return statement, the code already works. What's confusing here is that he thinks that C isn't defined.
I tried your code and it gives me the output "A=B=5" instead of just 5
ahhh i got you , want to pass argument and that argument be taken raw_input right
okay it worked! had to delete the A= and B= from the raw_input...thanks guys

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.