Question

In: Computer Science

python please take it out from class and make it all functions, add subtraction and multiplication...

python 
please take it out from class and make it all functions, add subtraction and multiplication functions with it.
than you so much for helping me out.



import random

image = 'w'


class cal():
    def __init__(self, a, b):
        self.a = a
        self.b = b

    def add(self):
        return self.a + self.b


a = random.randint(0, 9)
b = random.randint(0, 9)

obj = cal(a, b)
print("0. Exit")
print("1. Add")
choice = int(input("Enter choice: "))
cnt = 0  # To hold number of tries
if choice == 1:
    while True:
        print(" what do you think the sum of these two numbers are? ", a, " + ", b)
        print("This is how it looks viually", a * image, "+ ", b * image)

        sum = input()
        answer = int(sum)
        if answer == obj.add():
            print("Perfect, the answer is correct...")
            cont = input("\nIf you want to solve another question then press 1. If not then any other key ")
            if cont == "1":
                a = random.randint(0, 9)
                b = random.randint(0, 9)
                obj = cal(a, b)
                cnt = 0
                continue
            else:
                break

        elif answer != obj.add():
            print("I am sorry, your answer is wrong Sean,Please Try again:")
            # Incrementing count
            cnt = cnt + 1
            # Checking count
            if cnt == 3:
                # Prompting for next try
                ans = input(
                    "\nMax number of tries reached. Do you want to try another question or quit? (T/Q): ")
                # Checking answer
                if ans.upper() == "T":
                    a = random.randint(0, 9)
                    b = random.randint(0, 9)
                    obj = cal(a, b)
                    continue
                else:
                    break

            else:
                continue

Solutions

Expert Solution

CODE:

import random

image = 'w'
#modified functions which accepts two numbers each and returns the respective
#output
def add(a,b):
return a + b

def multiply(a,b):
return a*b

def subtract(a,b):
return a-b

a = random.randint(0, 9)
b = random.randint(0, 9)

print("0. Exit")
print("1. Add")
print("2. Multiply")
print("3. Subtract")
choice = int(input("Enter choice: "))
cnt = 0 # To hold number of tries
if choice == 1:
while True:
print(" what do you think the sum of these two numbers are? ", a, " + ", b)
print("This is how it looks viually", a * image, "+ ", b * image)

sum = input()
answer = int(sum)
if answer == add(a,b):
print("Perfect, the answer is correct...")
cont = input("\nIf you want to solve another question then press 1. If not then any other key ")
if cont == "1":
a = random.randint(0, 9)
b = random.randint(0, 9)
cnt = 0
continue
else:
break

elif answer != add(a,b):
print("I am sorry, your answer is wrong Sean,Please Try again:")
# Incrementing count
cnt = cnt + 1
# Checking count
if cnt == 3:
# Prompting for next try
ans = input(
"\nMax number of tries reached. Do you want to try another question or quit? (T/Q): ")
# Checking answer
if ans.upper() == "T":
a = random.randint(0, 9)
b = random.randint(0, 9)
continue
else:
print('Bye!')
break

else:
continue
elif choice == 2:
while True:
print(" what do you think the product of these two numbers are? ", a, " * ", b)
print("This is how it looks viually", a * image, " * ", b * image)
#same as addition
sum = input()
#asking for the answer
answer = int(sum)
#using the multiply function
if answer == multiply(a,b):
print("Perfect, the answer is correct...")
cont = input("\nIf you want to solve another question then press 1. If not then any other key ")
if cont == "1":
#if the user entered correctly
a = random.randint(0, 9)
b = random.randint(0, 9)
cnt = 0
continue
else:
break
#if the user did not answer correctly
elif answer != multiply(a,b):
print("I am sorry, your answer is wrong Sean,Please Try again:")
# Incrementing count
cnt = cnt + 1
# Checking count
if cnt == 3:
# Prompting for next try
ans = input(
"\nMax number of tries reached. Do you want to try another question or quit? (T/Q): ")
# Checking answer
if ans.upper() == "T":
a = random.randint(0, 9)
b = random.randint(0, 9)
continue
else:
print('Bye!')
break

else:
continue
elif choice == 3:
while True:
#same as addition
#displaying two random numbers
print(" what do you think the sum of these two numbers are? ", a, " - ", b)
print("This is how it looks viually", a * image, " - ", b * image)
  
sum = input()
answer = int(sum)
#asking for the answer
if answer == subtract(a,b):
print("Perfect, the answer is correct...")
cont = input("\nIf you want to solve another question then press 1. If not then any other key ")
if cont == "1":
#if the user continues
a = random.randint(0, 9)
b = random.randint(0, 9)
cnt = 0
continue
else:
break
#if the user is wrong
elif answer != subtract(a,b):
print("I am sorry, your answer is wrong Sean,Please Try again:")
# Incrementing count
cnt = cnt + 1
# Checking count
if cnt == 3:
# Prompting for next try
ans = input(
"\nMax number of tries reached. Do you want to try another question or quit? (T/Q): ")
# Checking answer
if ans.upper() == "T":
a = random.randint(0, 9)
b = random.randint(0, 9)
continue
else:
print('Bye!')
break
else:
continue

____________________________________________

CODE IMAGES:

_________________________________________________

OUTPUT:

___________________________________________

Feel free to ask any questions in the comments section

Thank You!


Related Solutions

There are four basic functions in math. They are addition, subtraction, multiplication and division. Children in...
There are four basic functions in math. They are addition, subtraction, multiplication and division. Children in 1st grade focus on addition. They are required to memorize their addition facts from 1 to 12. Create a function that prompts the user to enter a number between 1 and 12. Then display - using the input supplied - the addition and subtraction math facts for that number from 1 to 12 for the number entered. The output should be the results of...
There are four basic functions in math. They are addition, subtraction, multiplication and division. Children in...
There are four basic functions in math. They are addition, subtraction, multiplication and division. Children in 1st grade focus on addition. They are required to memorize their addition facts from 1 to 12. Create a function that prompts the user to enter a number between 1 and 12. Then display - using the input supplied - the addition and subtraction math facts for that number from 1 to 12 for the number entered. The output should be the results of...
Write a Python program that will perform various calculations (addition, subtraction, multiplication, division, and average). The...
Write a Python program that will perform various calculations (addition, subtraction, multiplication, division, and average). The program will add, subtract, multiply, or divide 2 numbers and provide the average of multiple numbers inputted from the user. You need to define a function named performCalculation which takes 1 parameter. The parameter will be the operation being performed (+,-,*,/). This function will perform the given prompt from the user for 2 numbers then perform the expected operation depending on the parameter that’s...
Please add to this Python, Guess My Number Program. Add code to the program to make...
Please add to this Python, Guess My Number Program. Add code to the program to make it ask the user for his/her name and then greet that user by their name. Please add code comments throughout the rest of the program If possible or where you add in the code to make it ask the name and greet them before the program begins. import random def menu(): print("\n\n1. You guess the number\n2. You type a number and see if the...
I have listed below two functions. Please add these two functions to StudentMath class. Test it...
I have listed below two functions. Please add these two functions to StudentMath class. Test it from the Test class public int divideByZero(int gradeA, int gradeB, int gradeC) { int numberOfTests = 0; int gradeAverage = 0; try { gradeAverage = (gradeA + gradeB + gradeC)/numberOfTests; } catch(ArithmeticException ex) { System.out.println("Divide by zero exception has occured" + ex.getMessage()); } return gradeAverage; } public int ArrayOutOfBoundEception() { int i = 0; try { // array of size 4. int[] arr =...
Not all operators are commutative, namely, a*b = b*a. Consider functions under the operators addition, subtraction,...
Not all operators are commutative, namely, a*b = b*a. Consider functions under the operators addition, subtraction, multiplication, division, and composition. Pick two of the functions and use a grapher (like Desmos) to graph the functions under the operators. If the functions are commutative under the operator, the graphs should be identical (overlap everywhere) when you change the order in which the functions are entered with the operator. Determine which operators seem to the commutative and which operators seem not to...
For this program you will add and test 2 new member functions to the class in...
For this program you will add and test 2 new member functions to the class in //************************ intSLList.h ************************** // singly-linked list class to store integers #ifndef INT_LINKED_LIST #define INT_LINKED_LIST class IntSLLNode { public: IntSLLNode() { next = 0; } IntSLLNode(int el, IntSLLNode *ptr = 0) { info = el; next = ptr; } int info; IntSLLNode *next; }; class IntSLList { public: IntSLList() { head = tail = 0; } ~IntSLList(); int isEmpty() { return head == 0; }...
Hello, please answer all 3 questions, if possible! :) I will make sure to add 5...
Hello, please answer all 3 questions, if possible! :) I will make sure to add 5 star rating! 29. Which of the following commands may be used to display boot error messages? (Choose all that apply.) a. dmesg | less b. less /var/log/boot c. less /var/log/wtmp d. less /var/log/boot.log 31. Which of the following can be used to obtain detailed performance statistics regarding virtual memory usage? (Choose all that apply.) a. Files stored within the /proc directory. b. The iostat...
1. Please create a New Class with the Class Name: Class17Ex Please add the ten methods:...
1. Please create a New Class with the Class Name: Class17Ex Please add the ten methods: 1. numberOfStudents 2. getName 3. getStudentID 4. getCredits 5. getLoginName 6. getTime 7. getValue 8. getDisplayValue 9. sum 10. max Show Class17Ex.java file with full working please. Let me know if you have any questions.
Answer in Python: show all code 3) Modify your code to take as input from the...
Answer in Python: show all code 3) Modify your code to take as input from the user the starting balance, the starting and ending interest rates, and the number of years the money is in the account. In addition, change your code (from problem 2) so that the program only prints out the balance at the end of the last year the money is in the account. (That is, don’t print out the balance during the intervening years.) Sample Interaction:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT