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.
list
of values if one of them. 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. sum
an array. Python has built-in functions to do that. Just use sum(list)
. Built-in Functions 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.