Question

Python programming Write a program that prompts the user to input the three coefficients a, b,...

Python programming

Write a program that prompts the user to input the three coefficients a, b, and c of a quadratic equationax2+bx+c= 0.The program should display the solutions of this equation, in the following manner:

1. If the equation has one solution, display ONE SOLUTION:, followed by the solution, displayed with4 digits printed out after the decimal place.

2. If the equation has two real solutions, display TWO REAL SOLUTIONS:, followed by the two solutions, each displayed with4 digits printed out after the decimal place.

3. If the equation has solutions that are not real numbers, display COMPLEX SOLUTIONS:, followed by two solutions displayed in the forma+bi, where each a and b should be displayed with 4 digits printed out after the decimal place

.For example, a run might look like

Enter x^2 coefficient: 1

Enter x^1 coefficient: -2

Enter x^0 coefficient: 1

ONE SOLUTION: x = 1.0000

or

Enter x^2 coefficient: 3

Enter x^1 coefficient: 5

Enter x^0 coefficient: 1

TWO REAL SOLUTIONS: x = -0.2324 and x = -1.4343

or

Enter x^2 coefficient: 2.1

Enter x^1 coefficient: 4

Enter x^0 coefficient: 10

COMPLEX SOLUTIONS: x = -0.9524 - 1.9634i and x = -0.9524 + 1.9634i

In the last case, note that the letter that is printed out to represent the square root of−1 is the letter i,not the letter j!Python has some functions that produce complex values, but when you print these values, they will display the letter j to represent√−1. I don’t want that – so, instead, your program should calculate the imaginary coefficient, and then include code which prints out the string"i".Do not use the cmath module, the complex function, the .imag attribute,or any Python functions that perform replacements in strings.(If you don’t know what any of those are, don’t worry about it.)

Specifications: your program must

•ask for and accept coefficients from the user.

•assuming that the user enters a non-zero leading coefficient, display the type of solutions as above. (In the case where there is exactly one solution, it is acceptable if the program occasionally misidentifies it, although it should work with the example I’ve provided.)

•display all solutions with exactly 4 digits printed after each decimal (for complex solutions, 4 digits after the decimal for both real and imaginary parts).

•use the letter i to represent √−1 in output, not the letter j.

•not use the cmath module, the complex function, the .imag attribute, or any Python functions that perform replacements in strings.

Challenge: write the program so that it gives the full, correct solution set for x for any real numbers a, b, c that are input,even if the leading coefficient is 0. Warning: I will be extremely stingy about awarding full credit, so make sure it behaves correctly for every last case! (Aside from the caveat I mentioned in the second specification above.)

Homework Answers

Answer #1
a=float(input('Enter x^2 coefficient: '))
b=float(input('Enter x^1 coefficient: '))
c=float(input('Enter x^0 coefficient: '))

if a==0:
    print('ONE SOLUTION: x = {:.4f}'.format(-c/b))
else:
    D=b*b-4*a*c
    if abs(D)<10**(-6):
        print('ONE SOLUTION: x = {:.4f}'.format(-b/(2*a)))
    elif D>0:
        print('TWO REAL SOLUTIONS: x = {:.4f} and x = {:.4f}'.format((-b+D**.5)/(2*a),(-b-D**.5)/(2*a)))
    else:
        print('COMPLEX SOLUTIONS: x = {:.4f} - {:.4f}i and x = {:.4f} + {:.4f}i'.format(-b/(2*a),(-D)**.5/(2*a),-b/(2*a),(-D)**.5/(2*a)))

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a Python program that prompts a user for two integers and stores the two integers...
Write a Python program that prompts a user for two integers and stores the two integers the user types into the shell. Print the product, float division, integer division, remainder, sum, and difference of the two integers to the console. The entire equation must be printed (see below for formatting). Only display 2 digits after the decimal place for results that are floats. Include a module docstring that summarizes the program.. Sample Run 1: Enter an integer: 5 Enter another...
(Python Programming) Write a program that prompts a user for a positive integer and then uses...
(Python Programming) Write a program that prompts a user for a positive integer and then uses a loop to calculate and display the sum of specific fractions as follows: Let's say the user enters 5, then your program will compute: 1/5 + 2/4 + 3/3 + 4/2 + 5/1 which is 8.7.
Create a Python program that: Allows the user to enter a phrase or sentence. The program...
Create a Python program that: Allows the user to enter a phrase or sentence. The program should then take the phrase or sentence entered Separate out the individual words entered Each individual word should then be added to a list After all of the words have been place in a list Sort the contents of the list Display the contents of the sorted list with each individual word displayed on a separate line Display a message to the user indicating...
Write a java program that repeatedly prompts the user to input a string starting with letters...
Write a java program that repeatedly prompts the user to input a string starting with letters from the English alphabet. The program must stop getting input when the user inputs the string “STOOOOP”. Then the program must display the words starting with each letter from the alphabet in a separate line (e.g. you need to make a new line after all words starting with a specific letter are finished.
Write a program in python to display all the consonant in the user input. E.g. user...
Write a program in python to display all the consonant in the user input. E.g. user input: Hello Good Day to you. Output: consonant H = 1 consonant l = 2 consonant G = 1   consonant d = 1 consonant D = 1 etc
Write a Python program to ask user input of a string, then ask user input of...
Write a Python program to ask user input of a string, then ask user input of what letters they want to remove from the string. User can either put in "odd", "even" or a number "n" that is greater than 2. When user input "odd", it will remove the characters which have odd numbers in the sequence of the string. When user input "even", it will remove the characters which have even numbers in the sequence of the string. When...
design a program that prompts the user to enter a positive integer number and reads the...
design a program that prompts the user to enter a positive integer number and reads the user’s input. If the user input is not a positive number, the program should prompt them repeatedly until they enter a valid input. Once a valid input is received ,the program uses a loop to calculate the sum of the digits of the user’s input and displays the sum. For example, if the user enters the number 94311, the program should print the sum...
Using python, write the program below. Program Specifications: You are to write the source code and...
Using python, write the program below. Program Specifications: You are to write the source code and design tool for the following specification: A student enters an assignment score (as a floating-point value). The assignment score must be between 0 and 100. The program will enforce the domain of an assignment score. Once the score has been validated, the program will display the score followed by the appropriate letter grade (assume a 10-point grading scale). The score will be displayed as...
In Python write a program that prompts the user for six elements and their atomic numbers...
In Python write a program that prompts the user for six elements and their atomic numbers and stores them in a list. Each element and weight pair should also be a list. After the values have been entered, print the list as it entered. Then print the first two characters from the element names (the first character capitalized and the second in lower case) along with the atomic number, starting with the element with the lowest atomic number. You must...
Write a program In python of Jupiter notebook That prompts the user to enter his/her first...
Write a program In python of Jupiter notebook That prompts the user to enter his/her first name, last name, and year of birth in a single string (in the format: fn;ln;yyyy), then calculates the age tell it to him/her after greeting him/her using the first name. Make sure to capitalize the user's first name, regardless of how he/she typed it in. Hint: review Exercise 1! Example: Enter your first name, last name, and birth year (in format fn;ln;yyyy): alex;smith;1994 Hi...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT