Why does my code keep displaying an error - google colab assignment?

Viewed 26
def numbers():
  num = []
  more = 'yes'
  while more == 'yes':
    amount = input("Give me a number!")
    amount = int(amount)
    num.append(amount)
    more = input("Do you have anymore numbers you want to put in?")
    if more == 'no':
      print('ok')
    if more == 'yes':
      more = 'yes'
  return num

def Math(num):
  return min(num)
  return max(num)
  from statistics import mean
  return mean(num)

numbers()
Math1 = Math(num)

print("The minimum, maximum and average is", Math1)

#For some reason the code does not recognize my "num" list. Im not sure as to why because what its supposed to do is find the minimum, maximum, and average of the list that is filled with random numbers by the user.

NameError                                 Traceback (most recent call last)
<ipython-input-28-d9aba5506cc2> in <module>
     20 
     21 Numbers1 = numbers()
---> 22 Math1 = Math(num)
     23 
     24 print("The minimum, maximum and average is", Math1)

NameError: name 'num' is not defined
1 Answers

The Problem was that the syntax to return multiple values from a function is a bit diffrent. Also num is not a global variable but luckily it is returned from numbers so we can acutally access it directly via a method call.

def numbers():
  num = []
  more = 'yes'
  while more == 'yes':
    amount = input("Give me a number!")
    amount = int(amount)
    num.append(amount)
    more = input("Do you have anymore numbers you want to put in?")
    if more == 'no':
      print('ok')
    if more == 'yes':
      more = 'yes'
  return num

def Math(num):
  from statistics import mean
  return min(num), max(num), mean(num)

Math1 = Math(numbers())

print("The minimum, maximum and average is", Math1)
Related