Home > database >  Calling a Python Function Without Entering a New Input
Calling a Python Function Without Entering a New Input

Time:11-14

I am writing a program that takes an input and turns it into a list of integers, then outputting the minimum and maximum number in the list. When I call the function that turns the input into a list it asks for a new input. How can I fix this so the original input is stored?

def getList():
    user_input = input()
    newlist = user_input.split()

    for index in range(len(newlist)):
        newlist[index] = int(newlist[index])
    return newlist

def listMin(aList):
    alist = getList()
    small = alist[0]
    for num in alist:
        if num < small:
            small = num
    return small

def listMax(aList):
    alist = getList()
    large = alist[0]
    for num in alist:
        if num > large:
            large = num
    return large

### main program ###

myList = getList()
print(myList)
print(listMin(myList))
print(listMax(myList))

When this is run it asks for a new input for listMin and listMax.

CodePudding user response:

You should be using aList that you already pass as an argument into the functions, not calling getList() again. Simply remove the lines where you call getList() unnecessarily:

def listMin(aList):
    small = aList[0]
    for num in aList:
        if num < small:
            small = num
    return small

def listMax(aList):
    large = aList[0]
    for num in aList:
        if num > large:
            large = num
    return large

CodePudding user response:

It asks you for an input a second time because you are calling getList() again when you are inside the listMin function. By deleting alist = getList() you will get rid of that problem, but you should also change all the variable names of alist to aList or you should change the name of the parameter inside the listMin function.

  • Related