Question

In: Computer Science

Please fix all the errors in this Python program. import math def solve(a, b, c): """...

Please fix all the errors in this Python program. 

import math

def solve(a, b, c):
    """
        Calculate solution to quadratic equation and return
        @param coefficients a,b,class
        @return either 2 roots, 1 root, or None
    """

    #@TODO - Fix this code to handle special cases
    d = b ** 2 - 4 * a * c
    disc = math.sqrt(d)
    root1 = (-b + disc) / (2 * a)
    root2 = (-b - disc) / (2 * a)
    return root1, root2


if __name__ == '__main__':

    # This will work with given code
    # x^2+3x + 2 =0
    results = solve(1,3,2)
    print("Result of x^2+2x + 3 =0: "+str(results))

    # This does not work with given code
    # x^2+2x + 3 =0
    results = solve(1,2,3)
    print("Result of x^2+2x + 3 =0: "+str(results))

    while True:
        print()
        print("Input coefficients of quadratic equation: (ctrl-C to quit)")
        a = int(input("a: "))
        b = int(input("b: "))
        c = int(input("c: "))
        result = solve(a, b, c)
        print(result)

Solutions

Expert Solution

import math


def solve(a, b, c):
    """
        Calculate solution to quadratic equation and return
        @param coefficients a,b,class
        @return either 2 roots, 1 root, or None
    """
    d = b ** 2 - 4 * a * c
    if a == 0 or d < 0:
        return None
    disc = math.sqrt(d)
    root1 = (-b + disc) / (2 * a)
    root2 = (-b - disc) / (2 * a)
    if d == 0:
        return root1
    return root1, root2


if __name__ == '__main__':

    # This will work with given code
    # x^2+3x + 2 =0
    results = solve(1, 3, 2)
    print("Result of x^2+3x + 2 =0: " + str(results))

    # This does not work with given code
    # x^2+2x + 3 =0
    results = solve(1, 2, 3)
    print("Result of x^2+2x + 3 =0: " + str(results))

    while True:
        print()
        print("Input coefficients of quadratic equation: (ctrl-C to quit)")
        a = int(input("a: "))
        b = int(input("b: "))
        c = int(input("c: "))
        result = solve(a, b, c)
        print(result)

Related Solutions

Please fix all of the errors in this Python Code. import math """ A collection of...
Please fix all of the errors in this Python Code. import math """ A collection of methods for dealing with triangles specified by the length of three sides (a, b, c) If the sides cannot form a triangle,then return None for the value """ ## @TODO - add the errlog method and use wolf fencing to identify the errors in this code def validate_triangle(sides): """ This method should return True if and only if the sides form a valid triangle...
In Python Find the errors, debug the program, and then execute to show the output. def...
In Python Find the errors, debug the program, and then execute to show the output. def main():     Calories1 = input( "How many calories are in the first food?")     Calories2 = input( "How many calories are in the first food?")     showCalories(calories1, calories2)    def showCalories():     print('The total calories you ate today', format(calories1 + calories2,'.2f'))
in c++ please follow instructions and fix the errors and please make a comment next to...
in c++ please follow instructions and fix the errors and please make a comment next to each code error you fix. Below are 25 code fragments, inserted into a try catch block. Each line has zero or more errors. Your task is to find and remove all errors in each fragment. Do not fix problems by simply deleting a statement; repair the problems by changing, adding, or deleting a few characters. There may be different ways to fix them You...
Solve please in python b) Create a program that shows a series of numbers that start...
Solve please in python b) Create a program that shows a series of numbers that start at a and increase from 5 to 5 until reaching b, where a and b are two numbers captured by the user and assumes that a is always less than b. Note that a and b are not necessarily multiples of 5, and that you must display all numbers that are less than or equal to b. c) Create a program that displays n...
Can you fix the errors in this code? package demo; /** * * */ import java.util.Scanner;...
Can you fix the errors in this code? package demo; /** * * */ import java.util.Scanner; public class Booolean0p {        public class BooleanOp {            public static void main(String[] args) {                int a = 0, b = 0 , c = 0;                Scanner kbd = new Scanner(System.in);                System.out.print("Input the first number: ");                a = kbd.nextInt();                System.out.print("Input...
Please I seek assistance Python Programing import os import numpy as np def generate_assignment_data(expected_grade_file_path, std_dev, output_file_path):...
Please I seek assistance Python Programing import os import numpy as np def generate_assignment_data(expected_grade_file_path, std_dev, output_file_path): """ Retrieve list of students and their expected grade from file, generate a sampled test grade for each student drawn from a Gaussian distribution defined by the student expected grade as mean, and the given standard deviation. If the sample is higher than 100, re-sample. If the sample is lower than 0 or 5 standard deviations below mean, re-sample Write the list of student...
In Java please Cipher.java: /* * Fix me */ import java.util.Scanner; import java.io.PrintWriter; import java.io.File; import...
In Java please Cipher.java: /* * Fix me */ import java.util.Scanner; import java.io.PrintWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; public class Cipher { public static final int NUM_LETTERS = 26; public static final int ENCODE = 1; public static final int DECODE = 2; public static void main(String[] args) /* FIX ME */ throws Exception { // letters String alphabet = "abcdefghijklmnopqrstuvwxyz"; // Check args length, if error, print usage message and exit if (args.length != 3) { System.out.println("Usage:\n"); System.out.println("java...
Can you fix the errors in this code? import java.util.Scanner; public class Errors6 {    public...
Can you fix the errors in this code? import java.util.Scanner; public class Errors6 {    public static void main(String[] args) {        System.out.println("This program will ask the user for three sets of two numbers and will calculate the average of each set.");        Scanner input = new Scanner(System.in);        int n1, n2;        System.out.print("Please enter the first number: ");        n1 = input.nextInt();        System.out.print("Please enter the second number: ");        n2 =...
fix this code in python and show me the output. do not change the code import...
fix this code in python and show me the output. do not change the code import random #variables and constants MAX_ROLLS = 5 MAX_DICE_VAL = 6 #declare a list of roll types ROLLS_TYPES = [ "Junk" , "Pair" , "3 of a kind" , "5 of a kind" ] #set this to the value MAX_ROLLS pdice = [0,0,0,0,0] cdice = [0,0,0,0,0] #set this to the value MAX_DICE_VAL pdice = [0,0,0,0,0,0] cdice = [0,0,0,0,0,0] #INPUT - get the dice rolls i...
Please answer all a,b,c,d!!! Assume calendar year-ends for all companies and that all errors are material....
Please answer all a,b,c,d!!! Assume calendar year-ends for all companies and that all errors are material. a. Quigley Down Under Co. bought a machine on January 1, 2017 for $1,400,000. The machine had an estimated residual value of $120,000 and a ten-year life. “Machine expense” was debited on the purchase date for $1,400,000. Quigley uses straight-line depreciation for all assets. The error was discovered on June 15, 2018 after the books had been closed for 2017. What journal entry (if...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT