Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Homework5 #19

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions python5 hw copy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#Write a function named sum_values that takes a dictionary named my_dictionary as a parameter. The function should return the sum of the values of the dictionary
def sum_values (my_dictionary):
sum = 0
for i in my_dictionary.values():
sum = sum + i
return sum
my_dictionary = {"milk":5, "eggs":2, "flour": 3}
print(sum_values(my_dictionary))
my_dictionary = {10:1, 100:2, 1000:3}
print (sum_values(my_dictionary))

#Create a function called sum_even_keys that takes a dictionary named my_dictionary, with all integer keys and values, as a parameter. This function should return the sum of the values of all even keys.
def sum_even_keys (my_dictionary):
count = 0
for key in my_dictionary.keys():

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

тут має бути перевірка того, парне чи не парне цей ключ

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ісправіла)

if key % 2 == 0:
count += my_dictionary[key]
return count
my_dictionary = {1:5, 2:2, 3:3}
print(sum_even_keys(my_dictionary))
my_dictionary = {10:1, 100:2, 1000:3}
print(sum_even_keys(my_dictionary))

#Create a function named add_ten that takes a dictionary with integer values named my_dictionary as a parameter. The function should add 10 to every value in my_dictionary and return my_dictionary
def add_ten(my_dictionary):
for key in my_dictionary:
my_dictionary[key] += 10
return my_dictionary
my_dictionary = {1:5, 2:2, 3:3}
print(add_ten(my_dictionary))
my_dictionary = {10:1, 100:2, 1000:3}
print(add_ten(my_dictionary))

#Create a function named values_that_are_keys that takes a dictionary named my_dictionary as a parameter. This function should return a list of all values in the dictionary that are also keys.
def values_that_are_keys (my_dictionary):
list1 = []
for i in my_dictionary.values():
if i in my_dictionary.keys():
list1.append(i)
return list1
my_dictionary = {1:100, 2:1, 3:4, 4:10}
print(values_that_are_keys(my_dictionary))
my_dictionary = {"a":"apple", "b":"a", "c":100}
print(values_that_are_keys(my_dictionary))

#Write a function named max_key that takes a dictionary named my_dictionary as a parameter. The function should return the key associated with the largest value in the dictionary.
def max_key (my_dictionary) -> object:
max_key = 0
for key,value in my_dictionary.items():
if value > my_dictionary.get(max_key,0):
max_key = key
return max_key
my_dictionary = {1:100, 2:1, 3:4, 4:10}
print(max_key(my_dictionary))
my_dictionary = {"a":100, "b":10, "c":1000}
print(max_key(my_dictionary))