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'))
q7.4 Fix the errors in the code (in C) //This program is supposed to scan 5...
q7.4 Fix the errors in the code (in C) //This program is supposed to scan 5 ints from the user //Using those 5 ints, it should construct a linked list of 5 elements //Then it prints the elements of the list using the PrintList function #include <stdio.h> struct Node{ int data; Node* next; }; int main(void){ struct Node first = {0, 0}; struct Node* second = {0, 0}; Node third = {0, 0}; struct Node fourth = {0, 0}; struct...
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...
q7.1 Fix the errors in the code (in C) //This program should read a string from...
q7.1 Fix the errors in the code (in C) //This program should read a string from the user and print it using a character pointer //The program is setup to use pointer offset notation to get each character of the string #include <stdio.h> #include <string.h> int main(void){ char s[1]; scanf(" %c", s); char *cPtr = s[1]; int i=0; while(1){ printf("%c", cPtr+i); i++; } printf("\n"); }
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 =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT