Reputation: 3
The show_list
function of my code does not work. I get a message stating that 'multiples'
is not defined but I have not been able to identify the problem. Can someone please review and advice as to what it is Im doing wrong.
def main():
input1 = int(input("enter the low integer: "))
input2 = int(input("enter the high integer: "))
input3 = int(input("enter the integer for the multiples: "))
show_multiples(input1, input2, input3)
print ("List was created")
def show_multiples(input1, input2, input3):
num_range = range(input2, input1, -1)
multiples = []
for num in num_range:
if num % input3 == 0:
multiples.append(num)
return multiples
show_list(multiples)
def show_list(multiples):
elem = len(multiples)
average = sum(multiples) / elem
num_range = range(input2, input1, -1)
print ("the list has", elem, "elements.")
for num in num_range:
if num % input3 == 0:
print (num, end=" ")
print ("Average of multiples is ", average)
main()
Upvotes: 0
Views: 64
Reputation: 180492
You call show_list(multiples)
before you define the function show_list
Put your main function at the end of your code and call main() to run it:
def main():
input1 = int(input("enter the low integer: "))
input2 = int(input("enter the high integer: "))
input3 = int(input("enter the integer for the multiples: "))
show_multiples(input1, input2, input3)
print ("List was created")
main()
To just call show_list(multiples)
move it below where show_list
is defined
You will have more problems though:
def show_list(multiples):
elem = len(multiples)
average = elem / sum(multiples)
print ("the list has", elem, "elements.")
# num_range not defined only exists in show_multiples and input3 is also not accessable
for num in num_range:
if num % input3 == 0:
multiples.append(num)
print (num)
Not entirely sure what you want but I imagine this will get you closer:
input1 = int(input("enter the low integer: "))
input2 = int(input("enter the high integer: "))
input3 = int(input("enter the integer for the multiples: "))
num_range = range(input2, input1, -1)
def show_multiples():
multiples = []
for num in num_range:
if num % input3 == 0:
multiples.append(num)
return multiples
def show_list():
multiples = show_multiples()
elem = len(multiples)
average = elem / sum(multiples)
print ("the list has", elem, "elements.")
for num in num_range:
if num % input3 == 0:
multiples.append(num)
print (num)
show_list()
Upvotes: 1
Reputation: 61
mutliples
isn't defined in the global scope, only in the scope of show_multiples()
What you likely want to do is in the global scope, change
show_multiples(input1, input2, input3)
to
multiples = show_multiples(input1, input2, input3)
Upvotes: 0