Question

In: Computer Science

Programming language Python It have to be in Functions with a main function Samuel is a...

Programming language Python

It have to be in Functions with a main function

Samuel is a math teacher at Hogwarts School of Witchcraft and Wizardry. He loves to give his students multiplication exercises. However, he doesn’t care about the actual operation result but the unit sum of its digits.
At Hogwarts School of Witchcraft and Wizardry, they define the unit sum (US) of N as the unit that it is left after doing the sum of all the digits of a number over and over again until they get a number of exactly 1 digit. For example: US(976) = 9 + 7 + 6 = 22 -> 2 + 2 = 4. Another example is US(19) = 1.
Samuel left as an assignment to get the US of any number. In order to check if a student get the right answer or not, he wants to make a program that solves it fast enough, but he is too busy for doing it himself.
¿Can you help Samuel to check his homework?

Solutions

Expert Solution

Python code:

#defining unit sum function
def us(number):
    #looping till the number is a single digit
    while(len(number)!=1):
        #converting the number(which is a string) into a list of character and mapping it into
        #a list of integer and finding the sum of the list ans converting the sum to string and assigning it to number
        number=str(sum(map(int,list(str(number)))))
    #returning number
    return number
def main():
    #asking for number from user
    number=input("Enter the number: ")
    #asking for unit sum from user
    ans=input("Enter its unit sum: ")
    #checking if answer and unit sum are same
    if(ans==us(number)):
        #printing correct answer and the unit sum
        print("Congrats "+ans+" is the correct answer")
    else:
        #printing wrong answer and the unit sum
        print(ans+" is the wrong answer, correct answer is "+us(number))
if __name__ == "__main__":
    main()

Screenshot:


Input and Output:


Related Solutions

Language Python with functions and one main function Write a program that converts a color image...
Language Python with functions and one main function Write a program that converts a color image to grayscale. The user supplies the name of a file containing a GIF or PPM image, and the program loads the image and displays the file. At the click of the mouse, the program converts the image to grayscale. The user is then prompted for a file name to store the grayscale image in.
The programming language is Python Instructions: Create a function that will delete a node in a...
The programming language is Python Instructions: Create a function that will delete a node in a Linked List based on position number. On below example, if you want to delete position #2, it will remove the Banana (arrangement of nodes below is Apple, Banana, Cherry, Grapes, Orange). myLinkedList = LinkedList() myLinkedList.append("Banana") myLinkedList.append("Cherry") myLinkedList.append("Grapes") myLinkedList.append("Orange") myLinkedList.prepend("Apple") myLinkedList.deleteByPositionNum(2) node = myLinkedList.head while node: print(node.value, " ") node = node.next_node You may start with the function head: def deleteByPositionNum(self, positionNum):
python 3.6: You will have two functions: Function 1: update(user_dictionary): This function will take a dictionary...
python 3.6: You will have two functions: Function 1: update(user_dictionary): This function will take a dictionary entry as user that has keys, ‘username’,’password’,’uid’, and ‘gid’ along with values. You will search if the username is already in your user_list that you get from your userfile.json file that you have created in your previous task. If the username in your user_dictionary is not in the list that is in your .json file then add the user_dictionary into the existing list and...
Programming language in Python Suppose, for Jane, n1 = 3, n2 = 4, and n3 =...
Programming language in Python Suppose, for Jane, n1 = 3, n2 = 4, and n3 = 5. Also suppose, Jane iterates the number from 1 to 15. At the beginning, Jane sets count to 0, and then proceeds iterating the number from 1 to 15 and for each iteration does the following: for 1, count is increased by 1 because it is not divisible by 3, 4, and 5; count is now: 1 for 2, count is increased by 2...
Programming language is in python 3 For this project, you will import the json module. Write...
Programming language is in python 3 For this project, you will import the json module. Write a class named NobelData that reads a JSON file containing data on Nobel Prizes and allows the user to search that data. It just needs to read a local JSON file - it doesn't need to access the internet. Specifically, your class should have an init method that reads the file, and it should have a method named search_nobel that takes as parameters a...
Programming language is python 3 For this project, you will import the json module. Write a...
Programming language is python 3 For this project, you will import the json module. Write a class named NeighborhoodPets that has methods for adding a pet, deleting a pet, searching for the owner of a pet, saving data to a JSON file, loading data from a JSON file, and getting a set of all pet species. It will only be loading JSON files that it has previously created, so the internal organization of the data is up to you. The...
a summary explaining the basic understanding of the following programming concepts using a language of python:...
a summary explaining the basic understanding of the following programming concepts using a language of python: •Variables •Arithmetic and Logical operations •Sequential coding (Structured programming •Decision structure (If statements) •Repetition structure •Functions with some coding demos inside visual studio code python IDE which can be sent as screenshot. preferably a typed summary please which can be written into powerpoint pleaseeeee ???
Use the Python programming language to complete below (I need the New Answer for this, and...
Use the Python programming language to complete below (I need the New Answer for this, and please provide the screenshot of the source code. Thank you!): A website requests an user to input his account password. Write a program to examize the validity of the password. The valid password must consists of: At least 1 letter in [a-z] At least 1 number in [0-9] At least a letter in [A-Z] (capital) At least one special letter in [$#@] At least...
Use the Python programming language to complete below (I need the New Answer for this, and...
Use the Python programming language to complete below (I need the New Answer for this, and please provide the screenshot of the source code. Thank you!): Write a function to seek for all even numbers and odd numbers in the middle of two number A and B. Print even and odd numbers in 1 and 2020 (including both these two numbers)
Write an IPO diagram and Python program that has two functions, main and determine_grade. main –...
Write an IPO diagram and Python program that has two functions, main and determine_grade. main – Should accept input of five numeric grades from the user USING A LOOP.   It should then calculate the average numeric grade.    The numeric average should be passed to the determine_grade function. determine_grade – should display the letter grade to the user based on the numeric average:        Greater than 90: A 80-89:                 B 70-79:                 C 60-69:              D Below 60:           F Modularity:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT