So this is for an assignment where we're supposed to write code that asks a user for a number of integers, they input that number, then it asks them to, one at a time, put those numbers in. It then is supposed to find the maximum and minimum of those numbers and give them back to the user. We're at a point in the program where we can't use infinity or lists as well. I managed to do the first half, but I can't understand how to separate the numbers to compare them and find the values. I know what's not working now has something to do with the loop, but I'm not sure where to go. I'm a beginner, so literally any help is appreciated.
Also for clarification, we're using Python.
int_num = int(input("How many integers would you like to enter? "))
total = 0
print("Please enter", int_num, "integers.")
for num in range(1, int_num 1):
integers = int(input())
if integers < min:
min_value = min
elif integers > max:
max_value = max
print("min:", min_value)
print("max:", max_value)
CodePudding user response:
So another way is that you can assume the first inserted number is your minimum/maximum value. Then iterate through the rest(n-1) integers:
int_num = int(input("How many integers would you like to enter? "))
print("Please enter", int_num, "integers.")
min_value = max_value = int(input())
for _ in range(int_num - 1):
n = int(input())
if n < min_value:
min_value = n
elif n > max_value:
max_value = n
print("min:", min_value)
print("max:", max_value)
Note-1: Don't use the built-in names as your variable name. (min
and max
)
Note-2: There is no point for having range(1, int_num 1)
Because you don't even use the loop variable.
Note-3: You're not gonna calculate the total
of the numbers, so delete that extra variable.