Find age by name in a dictionary Medium 138 6 hours ago Hey there! This problem might be…
- User
Find age by name in a dictionary Medium 138 6 hours ago Hey there! This problem might be a bit unpredictable, but give it a go and let us know how you do! Given a dictionary where the keys are names and the values are ages, write a program that takes a name as input and prints the corresponding age if the name exists in the dictionary, otherwise it prints 'Not found'. Sample Input 1:
Alice {'Alice': 25, 'Bob': 30, 'Charlie': 35}
Sample Output 1:
25
Sample Input 2:
David {'Alice': 25, 'Bob': 30, 'Charlie': 35}
Sample Output 2:
Not found
Hint by avatar Zj Lee As this is out of scope in theory, your facing is unexpcted, so, nothing wrong with you. Let us begin: The main problem here is you have no way to create a new dict by input() either using {} or dict() constructor when the INPUT is a Dict STRING but in a str TYPE which is not a dict type # you can ... Was this hint helpful? Write a program in Python 3
def get_age(name, ages): # Write your code here
name = input() ages = eval(input()) print(get_age(name, ages))
- Assistant
Here's a simple Python program that will take a name as input and a dictionary (given in a string format) and print the corresponding age if the name is in the dictionary, or "Not found" if it isn't. The program will use the
eval()function to convert the input string to a dictionary, but remember that usingeval()can be dangerous if you're not sure of the input source. In a controlled environment like this problem, it's acceptable for the purpose of demonstration.def get_age(name, ages): return ages.get(name, 'Not found') # Take inputs from user name = input("Enter name: ") ages = eval(input("Enter dictionary of ages: ")) # Print the age or 'Not found' print(get_age(name, ages))Explanation
- User
When Anton finished reading "War and Peace", he decided to find out the number of specific words used in the book. Help Anton write a simplified version of such a program that will be capable of counting the words separated by spaces and print the statistics.
The code should:
Prompt the user to input a sentence.
Count the occurrence of each unique word in the sentence.
Print the results, showing each word and its frequency.
Additional details:
Treat the input as case-insensitive (e.g., "The" and "the" should be counted as the same word).
Words are assumed to be separated by spaces.
Each word should be printed only once in the output, regardless of its frequency.
The output format for each word should be: word frequency (e.g., sun 3).
The order of words in the output doesn't matter.
Sample Input 1:
a aa abC aa ac abc bcd a
Sample Output 1:
a 2 aa 2 abc 2 ac 1 bcd 1
Sample Input 2:
a A a
Sample Output 2:
a 3
Hint by avatar Charly
- Input the sentence.
- Split the sentence in words(make sure all the words are converted to lowercase). 3.Iterate through the word and add count to a dictionary.
- Print the iteration of the dictionary Was this hint helpful? Write a program in Python 3
- Assistant
Certainly! Below is a Python program that reads a sentence from the user, counts the frequency of each unique word regardless of case, and prints the word alongside its count.
def count_word_frequencies(sentence): # Convert the entire sentence to lowercase to handle case insensitivity sentence = sentence.lower() # Split the sentence into words words = sentence.split() # Create a dictionary to store the word counts word_count = {} # Count each word for word in words: if word in word_count: word_count[word] += 1 else: word_count[word] = 1 # Print the word frequencies for word, count in word_count.items(): print(f"{word} {count}") # Prompt the user to input a sentence sentence = input("Enter a sentence: ") # Call the function with the input sentence count_word_frequencies(sentence) - User
When Anton finished reading "War and Peace", he decided to find out the number of specific words used in the book. Help Anton write a simplified version of such a program that will be capable of counting the words separated by spaces and print the statistics.
The code should:
Prompt the user to input a sentence.
Count the occurrence of each unique word in the sentence.
Print the results, showing each word and its frequency.
Additional details:
Treat the input as case-insensitive (e.g., "The" and "the" should be counted as the same word).
Words are assumed to be separated by spaces.
Each word should be printed only once in the output, regardless of its frequency.
The output format for each word should be: word frequency (e.g., sun 3).
The order of words in the output doesn't matter.
Sample Input 1:
a aa abC aa ac abc bcd a
Sample Output 1:
a 2 aa 2 abc 2 ac 1 bcd 1
Sample Input 2:
a A a
Sample Output 2:
a 3
Hint by avatar Charly
- Input the sentence.
- Split the sentence in words(make sure all the words are converted to lowercase). 3.Iterate through the word and add count to a dictionary.
- Print the iteration of the dictionary Was this hint helpful? Write a program in Python 3
- Assistant
Certainly! Here's a Python program that meets the requirements specified:
def count_word_frequencies(sentence): # Convert the entire sentence to lowercase sentence = sentence.lower() # Split the sentence into words words = sentence.split() # Create a dictionary to store word counts word_count = {} # Count the frequency of each word for word in words: if word in word_count: word_count[word] += 1 else: word_count[word] = 1 # Print each word with its frequency for word, count in word_count.items(): print(f"{word} {count}") # Prompt the user to input a sentence sentence = input("Enter a sentence: ") # Call the function to count word frequencies count_word_frequencies(sentence)