126k views
5 votes
create your own min function that finds the minimum element in a list and use it in a separate function

1 Answer

7 votes

Answer:

def minfunction(mylist):

min = mylist[0]

for i in mylist:

if i < min:

min = i

print("The minimum is: "+str(min))

def anotherfunction():

mylist = []

n = int(input("Length of list: "))

for i in range(n):

listelement = int(input(": "))

mylist.append(listelement)

minfunction(mylist)

anotherfunction()

Step-by-step explanation:

This solution is implemented in Python

This defines the min function

def minfunction(mylist):

This initializes the minimum element to the first index element

min = mylist[0]

This iterates through the list

for i in mylist:

This checks for the minimum

if i < min:

... and assigns the minimum to variable min

min = i

This prints the minimum element

print("The minimum is: "+str(min))

This defines a separate function. This separate function is used to input items into the list

def anotherfunction():

This defines an empty list

mylist = []

This prompts user for length of list

n = int(input("Length of list: "))

The following iteration inputs elements into the list

for i in range(n):

listelement = int(input(": "))

mylist.append(listelement)

This calls the minimum function

minfunction(mylist)

The main starts here and this calls the separate function

anotherfunction()

User Bobby Orndorff
by
8.9k points

No related questions found

Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.