'Problem with minimum and maximum inside function (Python)
everyone, I wrote this code and can't understand how to add minimum and maximum of sequence in return. I tried min(sequence) and other things. Nothing is working. Please, help.
MONTHS = 4
def main():
precip = [0] * MONTHS
print(func(precip))
def func(sequence):
total = 0
for count in sequence:
count = int(input('Quantity: '))
total += count
average = total/len(sequence)
return total, average
main()
I found this code but i don't understand how to put it inside mine.
lst = [1, 0, 3, 6, -5, 7]
mins = lst[0]
for i in lst:
if i < mins:
mins = i
print(mins)
Solution 1:[1]
I'm guessing you want to fill a list with user input one by one?
Your approach to filling the items in the array is not quite right, you need to write the values directly into the array and not to the element created by the for loop. This is probably why the other things didn't work.
If sequence is a list, then the inbuilt min max should work.
def func(sequence):
for i in range(len(sequence)):
sequence[i] = (int(input('Quantity: ')))
total = sum(sequence)
average = total/len(sequence)
seq_min = min(sequence)
seq_max = max(sequence)
return total, average, seq_min, seq_max
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
