简体   繁体   中英

How to pass multiple inputs from the user to a function?

I'm trying to get the number of tests, assignments, quizzes and labs. Then pass those values to a function to get the score for each item in the category.

How do I pass multiple values so they can be used in the function?

def get_initial_input():

    int(input("How many assignments? "))
    int(input("How many quizzes? "))
    int(input("How many labs? "))
    int(input("How many tests? "))

    #Trying to pass all the values entered above to the function below
    #Using 'return' I can only pass one value

def get_scores(s):


    for x in range(len(s)):
        s[x] = int(input("Give a score: "))

def main():

    num = get_initial_input()
    scores = [0] * num

    get_scores(scores)

    total = 0
    for x in range(len(scores)):
        total += scores[x]

    print("The sum is: "+str(total))

    if total > 0:
        print("The average is: "+str(total/num))

    if (total/num) > 100:
        print("You got extra credit!")

main()

You need to store the returned input:

assignments = int(input("How many assignments? "))

Now you have a variable you can work with. You'd need to return that from the function:

def get_initial_input():    
    assignments = int(input("How many assignments? "))
    quizzes = int(input("How many quizzes? "))
    labs = int(input("How many labs? "))
    tests = int(input("How many tests? "))
    return assignments, quizzes, labs, tests

and when store the return value of the function call:

assignments, quizzes, labs, tests = get_initial_input()

Assign them to a variable:

assigments = int(input("How many assignments? "))
quizzes = int(input("How many quizzes? "))

After you stored them, you can return the all:

return [assigment, quizzes,...]

Hope this helps!

def get_initial_input():
    input_list = []
    input_list.append(int(input("How many assignments? ")))
    input_list.append(int(input("How many quizzes? ")))
    input_list.append(int(input("How many labs? ")))
    input_list.append(int(input("How many tests? ")))
    return input_list

def get_scores(s):
    return [ int(input("Give a score: ")) for x in s]
    #use list comprehensions

def main():

    input_list = get_initial_input()
    scores = get_scores(input_list)

    total = sum(scores)
    num = sum(input_list) # I am assuming this

    print("The sum is: "+str(total))
    if total > 0:
        print("The average is: "+str(total/num))

    if (total/num) > 100:
        print("You got extra credit!")

main()

Points to remember.

  1. To return multiple values from a function, there are a lot of work arounds. Using a list of values if one of them.
  2. In the function get_scores , I have used a list comprehension. It can also be done using a for loop: for item in s: score_list.append(...); return score_list for item in s: score_list.append(...); return score_list . List comprehensions are much more cleaner and much more pythonic.
  3. You no longer have to write a loop to sum an array. Python has built-in functions to do that. Just use sum(list) . Built-in Functions
  4. I see that you are using a C/C++ like style of coding. Reading more tutorials and writing more code would help develop a pythonic style of writing code.

You can return any object capable of holding all the values.

Example with a tuple

def get_initial_input():
    return tuple( int(input("How many "+q+"? ")) \
        for q in ["assignments", "quizzes", "labs", "tests"] )

assignments, quizzes, labs, tests = get_initial_input()

See how you can assign the tuple to several variables.

Example with a dict:

def get_initial_input():
    res={}
    for q in ["assignments", "quizzes", "labs", "tests"]:
        res[q] = int(input("How many "+q+"? "))
    return res

config = get_initial_input()

Here you have a dict with all values entered by the user.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM