[英]Assistance with function not working
The show_list
function of my code does not work. 我的代码的
show_list
函数不起作用。 I get a message stating that 'multiples'
is not defined but I have not been able to identify the problem. 我收到一条消息,指出未定义
'multiples'
但我无法确定问题所在。 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()
You call show_list(multiples)
before you define the function show_list
在定义函数
show_list
之前,请调用show_list(multiples)
Put your main function at the end of your code and call main() to run it: 将main函数放在代码末尾,然后调用main()来运行它:
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 要仅调用
show_list(multiples)
请将其移动到定义了show_list
位置下方
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()
mutliples
isn't defined in the global scope, only in the scope of show_multiples() mutliples
不在全局范围内定义,仅在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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.