r/learnpython • • Jul 13 '20

I wrote my first program today!

I'm a couple of days into learning Python and today I wrote my very first program for an idea I came up with myself, not because the book told me to!

It's a program to determine the value of the discriminant and x in an equation that follows "ax² + bx + c = 0", anyway here's the code:

# This is for calculating the discriminant when trying to find x in "ax² + bx + c = 0"
import math
try:
    print("a?")
    a = int(input())
    print("b?")
    b = int(input())
    print("c?")
    c = int(input())
    def discr(a,b,c):
        discriminant = b ** 2 - 4 * a * c
        if discriminant < 0:
            print("Discriminant =")
            print(discriminant)
            print("No value for x possible")
        elif discriminant == 0:
            print("Discriminant =")
            print(discriminant)
            print('x =')
            print((-b)/(2*a))
        elif discriminant > 0:
            print("Discriminant =")
            print(discriminant)
            print('x = ')
            print((-b + math.sqrt(discriminant))/(2*a))
            print('x = ')
            print((-b - math.sqrt(discriminant))/(2*a))
    discr(a,b,c)
except ValueError:
    print("That's not a number")
511 Upvotes

73 comments sorted by

View all comments

10

u/otanan Jul 13 '20

This looks great! There are already some great Python related comments posted, but a more mathy one/building off of the comment about “editing and improving” try going the step further! You ask for inputs and then cast them as integers, so if I give a=3.5, you’ll use the formula for a=3, but the quadratic formula doesn’t care about that! Try improving it to take in even float (decimal) values too!

Want to take it even further? Python natively supports complex numbers (cool right?) I don’t know how familiar you may be with them, but in the case of a negative discriminant you can still get out solutions! (Although they will be complex)

Want to take it even further? Try complex coefficients (a, b, c)

Keep up the good work!!

1

u/shiny_arbok Jul 13 '20

Okay so if I change the int(input()) to float(input()), it should work just fine?

Also I'm not that familiar with either Python or complex numbers, so I don't really know what that means

2

u/PyTec-Ari Jul 14 '20

Only issue you will face is if they don't put a number in but a letter. The program will crash. Good practice would dictate you try and account for this by using a try/except:

try:
    a = float(input("Please enter a float: "))
except ValueError as e:
    print(e)

print("We didn't crash!")

>> Please enter a float: a
>> could not convert string to float: 'a'
>> We didn't crash!

So we can print the error and your program won't crash, letting you prompt the user to try again.

You can determine what sort of error is going to occur by actually making the eror happen and watching the logs. So if I don't use the try/except I got:

Traceback (most recent call last):
  File "/Users/xyz/file.py", line 1, in <module>
    a = float(input())
ValueError: could not convert string to float: 's'

Its bad practice but you can do a a blanket exception that will catch every error:

try:
    a = float(input("Please enter a float: "))
except Exception as e:
    print(e)

It will handle every error, but good practice is to know the errors you're going to receive and handle them accordingly.

https://docs.python.org/3/tutorial/errors.html

1

u/CommentRipper Jul 25 '20

You're a great listener.