0

I am trying to solve a programming task, and i have ran into some trouble. The task reads:

Consider the usual formula for computing solutions to the quadratic equation: ax2 + bx + c = 0 given by x = sqrt(b± b^2−4ac/2a) Write a program reads values for a,b and c from the command line. Use exceptions to handle missing arguments, and handle invalid input where b^2-4ac < 0

My program is as follows:

from math import sqrt

import sys

try:
    a = float(sys.argv[1])
    b = float(sys.argv[2])
    c = float(sys.argv[3])
    bac = b**2 - 4*a*c
    if bac < 0:
        raise ValueError
except IndexError:
    while True:
        input("No arguments read from command line!")
        a = float(input("a = ? "))
        b = float(input("b = ? "))
        c = float(input("c = ? "))
        bac = b**2 - 4*a*c
        if bac > 0:
            break
        if bac < 0:
            while True:
                print("Please choose values of a,b,c so\
                                         that b^2 - 4ac > 0")
                a = float(input("a = ? "))
                b = float(input("b = ? "))
                c = float(input("c = ? "))
                bac = b**2 - 4*a*c
                if bac > 0:
                    break
except ValueError: 
    while True:
        input("Please choose values of a,b,c so that b^2 - 4ac > 0")
        a = float(input("a = ? "))
        b = float(input("b = ? "))
        c = float(input("c = ? "))
        if bac > 0:
            break

for i in range(-1,2,2):     # i=-1, next loop > i=1
    x = (b + i*sqrt(bac)) / (2*a)
    print("x = %.2f"%(x))

It seems to be working fine, but in the case below it doesnt:

terminal >
python quadratic_roots_error2.py
No arguments read from command line!
a = ? 1
b = ? 1
c = ? 1
Please choose values of a,b,c so that b^2 - 4ac > 0
a = ? 5
b = ? 2
c = ? -3
No arguments read from command line!    
a = ? 5
b = ? 2
c = ? -3
x = -0.60
x = 1.00

Why does the program spit out the message "No arguments read from command line!"? I want the program to print every solution where b^2-4ac > 0, and whenever b^2-4ac < 0 i want the message "Please chose values of a,b,c so that b^2 - 4ac > 0" to be printed, like it does.

  • why not just put a loop statement that checks the nature of values inputted to the variables? – Surya Tej Sep 19 '18 at 17:17
  • That input-gathering code's a bit repetitive. See [here](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) for various efficient ways to get valid input. – PM 2Ring Sep 19 '18 at 17:24

3 Answers3

4

Why does the program spit out the message "No arguments read from command line!"?

Because it's trying to fetch arguments from the command line, and there aren't any.

The "command line" is the command you typed to start the program:

python quadratic_roots_error2.py

You have the option of supplying arguments when you type the command, like so:

python quadratic_roots_error2.py 5 99 1000

But you didn't do that, so the program prints that message and then reads input from the keyboard.

If you don't want that message to be printed, why is it even there?

John Gordon
  • 29,573
  • 7
  • 33
  • 58
  • Hi! I want the message to be printed if i dont supply commands. However, if you look at my terminal window it says "no arguments read from command line" after i have gone through the float(input("text")) exceptions. – Adrian Toftner Kølbæk Sep 21 '18 at 13:50
1

The IndexError you are getting comes from calling sys.argv[1] when there is nothing at the index of 1.

As John Gordon mentioned, sys.argv is looking for inputs given when the script itself is called. Since there are no arguments in the call, in this case python quadratic_roots_error2.py, then nothing is placed into the list sys.argv at index 1. Your script is then throwing an IndexError, which is being caught in your Except IndexError clause, and then going through the portion of your code that asks for the values of A, B, and C. The quickest way to fix your problem is to replace:

a = float(sys.argv[1])
b = float(sys.argv[2])
c = float(sys.argv[3])

with:

a = float(input("a = ? "))
b = float(input("b = ? "))
c = float(input("c = ? "))

like you have everywhere else in your script.

Nick
  • 4,302
  • 2
  • 24
  • 38
0

Thank you for the responses! I have taken them into consideration, and the code now looks like this:

try:
    a = float(sys.argv[1])
    b = float(sys.argv[2])
    c = float(sys.argv[3])
except IndexError:
    input("No arguments read from command line!")
    a = float(input("a = ? "))
    b = float(input("b = ? "))
    c = float(input("c = ? "))
try:
    for i in range(-1,2,2):
        x = (b + i*sqrt(b**2 - 4*a*c)) / (2*a)
        print("x = %.2f"%(x))
except ValueError:
    while True:
        input("Please choose values a,b,c so that b**2 - 4*a*c > 0")
        a = float(input("a = ? "))
        b = float(input("b = ? "))
        c = float(input("c = ? "))
        if b**2 - 4*a*c > 0:
            for i in range(-1,2,2):
                x = (b + i*sqrt(b**2 - 4*a*c)) / (2*a)
                print("x = %.2f"%(x))
            sys.exit(1)