Your error happens because you do not have any global name average
in scope when you use it.
You seem to be confused about when and whether to use the class
keyword. In your particular example, you don’t need it — both average
and main
want to be global functions, not class methods.
Try this program instead:
def average(a,b):
return int((a+b)/2)
def main():
num = input("Number? ")
x= int(num)
y= average(x+1,x)
print(y)
main()
Alternatively, if you want to learn about classes:
class two:
def __init__(self, x,y):
self.x = x
self.y = y
def average(self):
return (self.x + self.y)/2
def main():
t = two(7,42)
print(t.average())
main ()
Notice how the declaration of average
now includes a self
parameter — this links the call to a particular two
object. Notice also how the invocation of average
changed: it is now t.average()
. In this case, t
is the specific object which will be passed as the first parameter of two.average()
.
0
solved very new to python would appreciate [closed]