After calling both functions I’m getting ‘function object is not itrable’ as an error. I’m thinking the error appears somewhere in the for loop, I just can’t figure out why if I’m iterating through a list. The purpose of the program is to output the range of numbers between the first ‘input1’ and ‘upper_threshold’. Tyvm for your help.
def get_user_values(): user_values = [] input1 = int(input()) while input1 > 0: input2 = int(input()) user_values.append(input2) input1 -= 1 return user_values def output_ints_less_than_or_equal_to_threshold(user_values, upper_threshold): for value in user_values: if value >= min(user_values) and value <= upper_threshold: print (value) if __name__ == '__main__': user_values = [] get_user_values() upper_threshold = int(input()) print (output_ints_less_than_or_equal_to_threshold(get_user_values, upper_threshold))
Advertisement
Answer
I’m pretty sure that where you have:
print (output_ints_less_than_or_equal_to_threshold(get_user_values, upper_threshold))
you mean to have:
print (output_ints_less_than_or_equal_to_threshold(get_user_values(), upper_threshold))
In other words, you want to pass the results of calling the function get_user_values
to the function output_ints_less_than_or_equal_to_threshold
. Right now, you’re passing in the function itself, not something it returned. So your code is trying to iterate over the value user_values
that was passed to it, thinking it is a list of values. Instead, it gets and tries to iterate over a function.
The two other problems I see are that you aren’t providing any prompts when you accept user input, and you’re calling get_user_values()
twice, throwing the results away the first time you call it.
Here’s a version of your code with those two problems fixed. Also, your main function doesn’t return anything, so I removed the print()
that was printing None
as its result.With those fixes, it seems to do what I expect you want the code to do:
def get_user_values(): user_values = [] input1 = int(input("How many numbers?")) while input1 > 0: input2 = int(input("Enter a number")) user_values.append(input2) input1 -= 1 return user_values def output_ints_less_than_or_equal_to_threshold(user_values, upper_threshold): for value in user_values: if value >= min(user_values) and value <= upper_threshold: print (value) if __name__ == '__main__': upper_threshold = int(input("What threshold should I use?")) output_ints_less_than_or_equal_to_threshold(get_user_values(), upper_threshold)
Sample run:
What threshold should I use?5 How many numbers?4 Enter a number7 Enter a number2 Enter a number99 Enter a number3 2 3