Question

In: Computer Science

write pseudocode for this program . thank you import random class cal():    def __init__(self, a,...

write pseudocode for this program . thank you

import random

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

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

   def mul(self):
       return self.a * self.b

   def div(self):
       return self.a / self.b

   def sub(self):
       return self.a - self.b

def playQuiz():
   print("0. Exit")
   print("1. Add")
   print("2. Subtraction")
   print("3. Multiplication")
   print("4. Division")
   choice = int(input("Enter choice: "))

   # Generating random numbers
   a = random.randint(0, 9)
   b = random.randint(0, 9)
  
   obj = cal(a, b)
   while choice != 0:

       if choice == 1:
           print(" what do you think the sum of these two numbers are? ", a, " + ", b)
           cnt=0 # To hold number of tries
           while True:
               sum = input()
               answer = int(sum)
               if answer == obj.add():
                   print("Perfect, the answer is correct...")
                   return
               elif answer != obj.add():
                   print("I am sorry, your answer is wrong kitto,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":
                           return playQuiz()
                       else:
                           return
                      
       elif choice == 2:
           print(" what do you think the difference of these two numbers are? ", a, " - ", b)
           cnt=0 # To hold number of tries
           while True:
               subt = input()
               answer = int(subt)
               if answer == obj.sub():
                   print("Perfect, the answer is correct...")
                   return
               elif answer != obj.sub():
                   print("I am sorry, your answer is wrong kitto,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":
                           return playQuiz()
                       else:
                           return
       elif choice == 3:
           print(" what do you think the multiplication of these two numbers are? ", a, " * ", b)
           cnt=0 # To hold number of tries
           while True:
               mult = input()
               answer = int(mult)
               if answer == obj.mul():
                   print("Perfect, the answer is correct...")
                   return
               elif answer != obj.mul():
                   print("I am sorry, your answer is wrong kitto,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":
                           return playQuiz()
                       else:
                           return
       elif choice == 4:
           print(" what do you think the division of these two numbers are? ", a, " / ", b)
           cnt=0 # To hold number of tries
           while True:
               divt = input()
               answer = int(divt)
               if answer == obj.div():
                   print("Perfect, the answer is correct...")
                   return
               elif answer != obj.div():
                   print("I am sorry, your answer is wrong kitto,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":
                           return playQuiz()
                       else:
                           return
       elif choice == 0:
           print("Exiting!")
       else:
           print("Invalid choice!!")
   print()
  

# Calling function
playQuiz()

Solutions

Expert Solution

The pseudocode for the program is given below:

import random

create class cal

                create constructor that takes two values set it to a and b respectively

                create function add that return a+b

                create function mul that return a*b

                create function div that return a/b

                create function sub that return a-b

function playQuiz

                print "0. Exit"

                print "1. Add"

                print "2. Subtraction"

                print "3. Multiplication"

                print "4. Division"

                take choice from user convert it to int

                generate random number between 0 to 9 and store into a

                generate random number between 0 to 9 and store into b

                create object of class cal

                continue until choice not equal to 0

  if choice is 1

then print "what do you think the sum of these two numbers are? a + b "

set number of tries to 0

while true

take input from user(sum of a+b) into sum

convert sum to int and store into answer

if answer is same as the value that is returned by calling the function add of the class cal

then print "Perfect, the answer is correct..."and return

else if answer is not same as the value that is returned by calling the function add of the class cal

then print "I am sorry, your answer is wrong kitto,Please Try again:"

and increment number of tries by 1

and check if number of tries is equal to 3

then print Max number of tries reached.

and ask user Do you want to try another question or quit? (T/Q):

if user enter T then return PlayQuiz function

otherwise do return only

                                if choice is equal to 2

                                                then print "what do you think the difference of these two numbers are? a-b"

                                                set number of tries to 0

                                                while true

take input from user( eg. 3 which is subtraction of a-b) and convert it to int and store into answer

                                                                if answer is same as the value that is returned by calling the function sub of the class cal

                                                                                then print "Perfect, the answer is correct..."and return                                                                

else if answer is not same as the value that is returned by calling the function sub of the class cal

                                                                                then print "I am sorry, your answer is wrong kitto,Please Try again:"

                                                                                and increment number of tries by 1

                                                                                and check if number of tries is equal to 3

                                                                                                then print Max number of tries reached.

                                                                                                and ask user Do you want to try another question or quit? (T/Q):

                                                                                                if user enter T then return PlayQuiz function

                                                                                                otherwise do return only

                                if choice is equal to 3

                                                then print "what do you think the multiplication of these two numbers are? a*b"

                                                set number of tries to 0

                                                while true

take input from user(eg. 15 which is multiplication of a*b) and convert it to int and store into answer

                                                                if answer is same as the value that is returned by calling the function mul of the class cal

                                                                                then print "Perfect, the answer is correct..."and return                                                                                   

else if answer is not same as the value that is returned by calling the function mul of the class cal

                                                                                then print "I am sorry, your answer is wrong kitto,Please Try again:",

                                                                                and increment number of tries by 1

                                                                                and check if number of tries is equal to 3

                                                                                                then print Max number of tries reached.

and ask user Do you want to try another question or quit? (T/Q):

                                                                                                if user enter T then return playQuiz function like( return playQuiz())

                                                                                                otherwise do return only like (return)

                                if choice is equal to 4

                                                then print "what do you think the division of these two numbers are? a/b"

                                                set number of tries to 0

                                                while true

take input from user(eg.2 division of a/b) and convert it to int and store into answer

                                                                if answer is same as the value that is returned by calling the function div of the class cal

                                                                                then print "Perfect, the answer is correct..."and return                                                                                   

else if answer is not same as the value that is returned by calling the function div of the class cal

                                                                                then print "I am sorry, your answer is wrong kitto,Please Try again:"

                                                                                and increment number of tries by 1

                                                                                and check if number of tries is equal to 3

                                                                                                then print Max number of tries reached.

                                                                                                and ask user Do you want to try another question or quit? (T/Q):

                                                                                                if user enter T then return PlayQuiz function like( return playQuiz())

                                                                                                otherwise do return only like(return)

                                if choice is equal to 0

then print “Exiting!”

                                for any other choice

print” Invalid choice!!”

call function playQuiz


Related Solutions

For python... class car:    def __init__(self)          self.tire          self.gas    def truck(self,color)        &nb
For python... class car:    def __init__(self)          self.tire          self.gas    def truck(self,color)               style = color                return    def plane(self,oil)              liquid = self.oil + self.truck(color) For the plane method, I am getting an error that the class does not define __add__ inside init so I cannot use the + operator. How do I go about this.             
''' File: pyPatientLL.py Author: JD ''' class Node: #ADT        def __init__(self, p = None):             ...
''' File: pyPatientLL.py Author: JD ''' class Node: #ADT        def __init__(self, p = None):              self.name = ""              self.ss = self.age = int(0)              self.smoker = self.HBP = self.HFD = self.points = int(0)              self.link = None              #if list not empty              if p != None:                     p.link = self        ptrFront = ptrEnd = None choice = int(0) def menu():        print( "\n\tLL Health Clinic\n\n")        print( "1. New patient\n")        print( "2. View patient by...
1) What is the argument in “class AddressBook(object):” 2) What is the roll of “def __init__(self):”?...
1) What is the argument in “class AddressBook(object):” 2) What is the roll of “def __init__(self):”? 3) What is the roll of “def __repr__ (self):”? 4) Please copy and run “Addressbook” Python program. Submit the code and the output 5) Discuss the two outputs’ differences (data type). 6) Please add 2 more people and report the output. Code: class AddressBook(object): def init_(self): self.people=[] def add_entry(self, new_entry): self.people.append(new_entry) class AddressEntry(object): def __init__(self, first_name=None, family_name= None, email_address= None, DOB= None): self.first_name =...
"""stack.py implements stack with a list""" class Stack(object): def __init__(self): #creates an empty stack. O(1) self.top...
"""stack.py implements stack with a list""" class Stack(object): def __init__(self): #creates an empty stack. O(1) self.top = -1 #the index of the top element of the stack. -1: empty stack self.data = [] def push(self, item): # add item to the top of the stack. O(1) self.top += 1 self.data.append(item) def pop(self): # removes and returns the item at the top O(1) self.top -=1 return self.data.pop() def peek(self): # returns the item at the top O(1) return self.data[len(self.data)-1] def isEmpty(self):...
How would I setup this dictionary for Python 3? class Student(object): def __init__(self, id, firstName, lastName,...
How would I setup this dictionary for Python 3? class Student(object): def __init__(self, id, firstName, lastName, courses = None): The “id”, “firstName” and “lastName” parameters are to be directly assigned to member variables (ie: self.id = id) The “courses” parameter is handled differently. If it is None, assign dict() to self.courses, otherwise assign courses directly to the member variable. Note: The “courses” dictionary contains key/value pairs where the key is a string that is the course number (like “course1”) and...
Please I can get a flowchart and a pseudocode for this java code. Thank you //import...
Please I can get a flowchart and a pseudocode for this java code. Thank you //import the required classes import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class BirthdayReminder {       public static void main(String[] args) throws IOException {        // declare the required variables String sName = null; String names[] = new String[10]; String birthDates[] = new String[10]; int count = 0; boolean flag = false; // to read values from the console BufferedReader dataIn = new BufferedReader(new...
Please can I get a flowchart and pseudocode for this java code. Thank you. TestScore.java import...
Please can I get a flowchart and pseudocode for this java code. Thank you. TestScore.java import java.util.Scanner; ;//import Scanner to take input from user public class TestScore {    @SuppressWarnings("resource")    public static void main(String[] args) throws ScoreException {//main method may throw Score exception        int [] arr = new int [5]; //creating an integer array for student id        arr[0] = 20025; //assigning id for each student        arr[1] = 20026;        arr[2] = 20027;...
Inheritance - Method Calls Consider the following class definitions. class C1(): def f(self): return 2*self.g() def...
Inheritance - Method Calls Consider the following class definitions. class C1(): def f(self): return 2*self.g() def g(self): return 2 class C2(C1): def f(self): return 3*self.g() class C3(C1): def g(self): return 5 class C4(C3): def f(self): return 7*self.g() obj1 = C1() obj2 = C2() obj3 = C3() obj4 = C4() For this problem you are to consider which methods are called when the f method is called. Because the classes form part of an inheritance hierarchy, working out what happens will...
Inheritance - Method Calls Consider the following class definitions. class C1(): def f(self): return 2*self.g() def...
Inheritance - Method Calls Consider the following class definitions. class C1(): def f(self): return 2*self.g() def g(self): return 2 class C2(C1): def f(self): return 3*self.g() class C3(C1): def g(self): return 5 class C4(C3): def f(self): return 7*self.g() obj1 = C1() obj2 = C2() obj3 = C3() obj4 = C4() For this problem you are to consider which methods are called when the f method is called. Because the classes form part of an inheritance hierarchy, working out what happens will...
Class AssignmentResult An object that represents the result of an assignment. __init__(self, id:int, assignment: Assignment, grade:...
Class AssignmentResult An object that represents the result of an assignment. __init__(self, id:int, assignment: Assignment, grade: float): """ This will contain the ID of the student, the assignment that the student worked on and the grade the student received on the assignment. :param id: The ID of the student that created this Assignment result :param assignment: The Assignment that the student worked on. :param grade: A number between 0-1 representing the numerical grade the student received """ id(self) -> int:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT