Today, we are going to explore a Python code snippet that takes student scores as input and identifies which subject has the highest average score (best subject) and which has the lowest (worst subject).
This is a fantastic exercise for beginners to practice concepts like loops, dictionaries, user input, and simple statistics. Let's dive in!
def get_scores():
student_scores = {}
while True:
subject = input("Enter the subject name or 'q' to quit: ")
if subject.lower() == 'q':
break
scores = input("Enter the scores for this subject, separated by spaces: ")
scores = list(map(int, scores.split()))
student_scores[subject] = scores
return student_scores
def summarize_scores(student_scores):
average_scores = {}
for subject, scores in student_scores.items():
average = sum(scores) / len(scores)
average_scores[subject] = average
best_subject = max(average_scores, key=average_scores.get)
worst_subject = min(average_scores, key=average_scores.get)
return {'Best Subject': best_subject, 'Worst Subject': worst_subject}
# Get the scores from the user
student_scores = get_scores()
# Print the summary
print(summarize_scores(student_scores))
Code Explanation
1. get_scores() is a function that prompts the user to input the subject name and the corresponding scores. The scores are space-separated and converted from strings to integers using the `map` function. This function continues to ask for input until the user types 'q'.
2. summarize_scores(student_scores) is a function that calculates the average score for each subject and identifies the best and worst subjects based on these averages. It uses the `max` and `min` functions to find the subjects with the highest and lowest averages, respectively.
3. The user-provided scores are passed to the `summarize_scores` function, and the result is printed out.
Your Task
Here's an interesting challenge that involves using loops, dictionaries, and a bit of logic.
I would like you to create a Python program that counts the frequency of words in a sentence provided by the user. This will involve splitting the sentence into words, using a loop to go through each word, and a dictionary to keep track of the count of each word.
Here's a skeleton of how your code might look:
def count_words():
sentence = input("Enter a sentence: ")
words = sentence.split()
word_counts = {}
# Your code here
return word_counts
Your task is to fill in the commented section with code that iterates over the words list, and for each word, if it's not already in word_counts, adds it with a count of 1. If it is already in word_counts, it should increment the count by 1.
Once you're done, your program should be able to take a sentence like "the quick brown fox jumps over the lazy dog" and return a dictionary like {'the': 2, 'quick': 1, 'brown': 1, 'fox': 1, 'jumps': 1, 'over': 1, 'lazy': 1, 'dog': 1}.
This is a common task in the field of Natural Language Processing and serves as a great practice for manipulating strings and using dictionaries in Python. Good luck!
Answer:
def count_words():
sentence = input("Enter a sentence: ")
words = sentence.split()
word_counts = {}
for word in words:
if word in word_counts:
word_counts[word] += 1
else:
word_counts[word] = 1
return word_counts
print(count_words())
Comments