-4
def compa9():
    a=int(input("enter the first number"))
    b=int(input("enter the second number"))

compa9()
if a==b:
    a==a
    b==b
    print("they are equal")
if a>b:
    a==a
    b==b
    print (a,"is bigger and") (b,"is smaller")
if a<b:
    a==a
    b==b
    print  (b,"is bigger and") (a,"is smaller")

#i tried to write a program to find the which number is bigger or smaller in python but i am getting an error,what should i do??

2 Answers2

1

As the top comment indicates, defining a variable in a function does not make that variable accessible within the global scope. Here is one change to your function that results in the functionality that you seem to be expecting.

def compa9():
    a=int(input("enter the first number"))
    b=int(input("enter the second number"))

    if a==b:
        print("they are equal")
    if a>b:
        print(a,"is bigger and",b,"is smaller")
    if a<b:
        print(b,"is bigger and",a,"is smaller")

compa9()

Alternatively, I think it makes more sense to have compa9 take in the numbers of interest as inputs. In other words, I think you should do the following instead:

def compa9(a,b):
    if a==b:
        print("they are equal")
    if a>b:
        print(a,"is bigger and",b,"is smaller")
    if a<b:
        print(b,"is bigger and",a,"is smaller")

a=int(input("enter the first number"))
b=int(input("enter the second number"))
compa9(a,b)
Ben Grossmann
  • 4,387
  • 1
  • 12
  • 16
1

The variables must have been declared within the function or not declared.

In case you have it declared inside the function, you should use "return", as in the following example:

def function_exemple():
    a=int(input("enter the first number"))
    b=int(input("enter the second number"))
    return a,b

a,b = function_exemple()

if a==b:
    a==a
    b==b
    print("they are equal")
if a>b:
    a==a
    b==b
    print (a,"is bigger and") (b,"is smaller")
if a<b:
    a==a
    b==b
    print  (b,"is bigger and") (a,"is smaller")

CabralSJM
  • 11
  • 1