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

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...
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.
Python Please (The Fan class) Design a class named Fan to represent a fan. The class...
Python Please (The Fan class) Design a class named Fan to represent a fan. The class contains: ■ Three constants named SLOW, MEDIUM, and FAST with the values 1, 2, and 3 to denote the fan speed. ■ A private int data field named speed that specifies the speed of the fan. ■ A private bool data field named on that specifies whether the fan is on (the default is False). ■ A private float data field named radius that...
IN MIPS!!! Question: Change this program to use XOR and ADD and take out XORI and...
IN MIPS!!! Question: Change this program to use XOR and ADD and take out XORI and ADDI. data    str1:   .asciiz "Enter a number you want to negate: "    str2:   .asciiz "Your answer in Decimal is :"    str3:   .asciiz "\nAnswer in Hex format: "    .text    la $a0, str1    li $v0, 4    syscall       li $v0, 5    syscall    move $t0, $v0       xori $t0, 0xffffffff    addi $t0, $t0, 1   ...
Add the method getTelephoneNeighbor to the SmartPhone class. Make this method return a version of the...
Add the method getTelephoneNeighbor to the SmartPhone class. Make this method return a version of the phone number that's incremented. Given Files: public class Demo4 { public static void main(String[] args) { SmartPhone test1 = new SmartPhone("Bret", "1234567890"); SmartPhone test2 = new SmartPhone("Alice", "8059226966", "[email protected]"); SmartPhone test3 = new SmartPhone(); SmartPhone test4 = new SmartPhone("Carlos", "8189998999", "[email protected]"); SmartPhone test5 = new SmartPhone("Dan", "8182293899", "[email protected]"); System.out.print(test1); System.out.println("Telephone neighbor: " + test1.getTeleponeNeighbor()); System.out.println(); System.out.print(test2); System.out.println("Telephone neighbor: " + test2.getTeleponeNeighbor()); System.out.println(); System.out.print(test3); System.out.println("Telephone...
Python Please debug each python block. Do not change the algorithm. You can add statements or...
Python Please debug each python block. Do not change the algorithm. You can add statements or you can modify existing python statements. #2) The below code prints all numbers from 5 to 20. Modify the below while loop to print odd numbers from 5 to 21, # including 21. Correct syntax errors as well. i = 5 while(i<21): print(i) i = i+1
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT