Question

In: Computer Science

Design and implement a function which has one input parameter which is a number which is...

Design and implement a function which has one input parameter which is a number which is greater than 50, called num. Then the function will create a dictionary whose keys are 2 and 3 and 4 and 5 and 6 and 7 and 8 and 9. Then the function calculates the values for each of the above keys. The value for a key is all the numbers between 2 and input “num” that are divisible by the key. The function eventually will print the result. - Hint: Use a dictionary whose values are lists. - Example: num = 20 2: [2,4,6,8,10,12,14,16,18,20] 3: [3,6,9,12,15,18] 4: [4,8,12,16,20] 5: [5,10,15,20] 6: [6,12,18] 7: [7, 14] 8: [8, 16] 9: [9, 18]. write a python program for this question. use main function.

Solutions

Expert Solution

Python code:

#defining function which create required dictionary
def dictionary(num):
    #creating dictionary,checking if the number is divisible by 2
    dic={2:[i for i in range(2,num+1) if(i%2==0)],
    #checking if the number is divisible by 3
    3:[i for i in range(2,num+1) if(i%3==0)],
    #checking if the number is divisible by 4
    4:[i for i in range(2,num+1) if(i%4==0)],
    #checking if the number is divisible by 5
    5:[i for i in range(2,num+1) if(i%5==0)],
    #checking if the number is divisible by 6
    6:[i for i in range(2,num+1) if(i%6==0)],
    #checking if the number is divisible by 7
    7:[i for i in range(2,num+1) if(i%7==0)],
    #checking if the number is divisible by 8
    8:[i for i in range(2,num+1) if(i%8==0)],
    #checking if the number is divisible by 9
    9:[i for i in range(2,num+1) if(i%9==0)]}
    #printing the dictionary
    print(dic)
def main():
    #accepting number
    num=int(input("Enter the number: "))
    #calling dictionary function
    dictionary(num)

if __name__ == "__main__":
    main()


Screenshot:


Input and Output:


Related Solutions

Write a python code to Design and implement a function with no input parameter which reads...
Write a python code to Design and implement a function with no input parameter which reads a number from input (like 123). Only non-decimal numbers are valid (floating points are not valid). The number entered by the user should not be divisible by 10 and if the user enters a number that is divisible by 10 (like 560), it is considered invalid and the application should keep asking until the user enters a valid input. Once the user enters a...
- Design and implement a function with no input parameters. The function keeps receiving a number...
- Design and implement a function with no input parameters. The function keeps receiving a number from input (user) and adds the numbers together. The application keeps doing it until the user enter 0. Then the application will stop and print the total sum and average of the numbers the user had entered.
Please write the code in c++ Write a function with one input parameter that is a...
Please write the code in c++ Write a function with one input parameter that is a vector of strings. The function should count and return the number of strings in the vector that have either an 'x' or a 'z' character in them. For example, when the function is called, if the vector argument contains the 6 string values, "enter", "exit", "zebra", "tiger", "pizza", "zootaxy" the function should return a count of 4. ("exit", "zebra", "pizza", and "zootaxy" all have...
Write A function with an input parameter which is a string arithmetic statement which contains only...
Write A function with an input parameter which is a string arithmetic statement which contains only alphabet variables and binary operations including +, -, *, / and % and checks whether the statement is a valid arithmetic statement or not. If the statement is valid, the function returns true, otherwise returns false. • The statement might contain parenthesis as well. For instance: • a+b*a+c/c%y • (a+b)*(a/d-(a/b)) • You can make this assumption that the variable names contain only one alphabet...
\ Implement function find_key that takes a dictionary with int:int pairs and a second parameter which...
\ Implement function find_key that takes a dictionary with int:int pairs and a second parameter which is an int that corresponds to one of the dictionary values, and returns the key that corresponds to this value. If there are more than one keys, it returns the smallest key. If there is no key that maps to this value, the functions returns False. You may not use the .values and .items methods. You may not create lists, tuples, strings, sets, dictionaries....
- Write a function with no input parameter which keeps asking the user to enter positive...
- Write a function with no input parameter which keeps asking the user to enter positive numbers until the user enters an invalid input. (An invalid input is an input which includes at least one alphabet, like 123d4). The program should print the Max and Min of the numbers the user had entered as well as the distance between the Max and Min. (Remember to calculate the absolute distance). The function does not return anything
Define a function digits_stars(...) which receives as input one digit (that is, an integer number greater...
Define a function digits_stars(...) which receives as input one digit (that is, an integer number greater or equal than 0 and less or equal than 9). The function should return a string. The string will have , alternating, a digit and a "*" symbol, starting with the digit 0, then a star, then the digit 1, then a star, and so on, alternating until reaching the input digit and one star symbol to end the string. As an example, the...
(3) Implement the getNumOfNonWSCharacters() method. getNumOfNonWSCharacters() has a string as a parameter and returns the number...
(3) Implement the getNumOfNonWSCharacters() method. getNumOfNonWSCharacters() has a string as a parameter and returns the number of characters in the string, excluding all whitespace. Call getNumOfNonWSCharacters() in the main() method. (4 pts) Ex: Enter a sample text: We'll continue our quest in space. There will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes and our journeys continue! You entered: We'll continue our quest in space....
Design, implement, and fully test a Python3 function that converts a number to words (words_from_number(number: int))....
Design, implement, and fully test a Python3 function that converts a number to words (words_from_number(number: int)). It should expect to be passed a natural number (such as 12345) and return the corresponding words (i.e., “twelve thousand three hundred forty-five”). Since Python integers can grow arbitrarily large, your code should be flexible, handling at least 20 digits (realistically, it’s just as easy up to 30 digits). Spell everything correctly, and use correct punctuation (hyphens for forty-five and thirty-seven, no commas or...
Design, implement, and fully test a Python3 function that converts a number to words (words_from_number(number: int))....
Design, implement, and fully test a Python3 function that converts a number to words (words_from_number(number: int)). It should expect to be passed a natural number (such as 12345) and return the corresponding words (i.e., “twelve thousand three hundred forty-five”). Since Python integers can grow arbitrarily large, your code should be flexible, handling at least 20 digits (realistically, it’s just as easy up to 30 digits). Spell everything correctly, and use correct punctuation (hyphens for forty-five and thirty-seven, no commas or...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT