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/otanan Jul 13 '20

Yes for changing to floats!

As for the complex numbers, try looking into them, at a very basic level they serve as expanding on the real numbers (think, every normal number you work with, from -20, to 42.6423, to pi, to 10^100), where this expansion is done by allowing a definition of sqrt(-1).

In the case of solving the following quadratic: x^2+1=0, you've noticed that you'll get a negative discriminant and sure enough, if you just try math.sqrt(-1) you'll get an error, but complex numbers allow us to make sense of these kinds of things! If you allow complex numbers you can use the quadratic formula to solve any quadratic, no need for the error handling for negative discriminants!

Look into the cmath package. If you change the import math line to:

import cmath then use

cmath.sqrt(-1)

You'll get something different! It's a complex number! I claim that this number actually is the square root of -1! How can you check that this is true?

It's a lot of fun to play around with these kinds of things, and the great thing about programming is that you can dive into all kinds of rabbit holes just as soon as you've found them! Explore!

1

u/shiny_arbok Jul 13 '20

Oh THAT's what complex numbers are! I forgot for a moment, it's like, the complex number is named 'i', right? I've seen that in my last high school year in math class but it didn't really stick with me apparently

Also is programming really such a big rabbit hole? I usually have a hard time exiting rabbit holes, so that'd be a good thing in this case, right?

2

u/otanan Jul 13 '20

It's a great thing in this case ;)

Unfortunately one of the things that new programmers often struggle with (I definitely know that I did) is that we can learn all of these new concepts in theory, but we often have no idea how to play with them in practice. Sure I understand object inheritance now, but I want to write a program and I have no clue what to write. Exploring those rabbit holes is one way you're exercising your creativity and will help you really consolidate these new programming ideas (wait! an enemy and a follower in a game are just both examples of NPC's, maybe I can write an object called NPC that handles most of the code for them and have them each inherit from the NPC class)

You're doing exactly that with this example quadratic solver, keep going!

2

u/shiny_arbok Jul 13 '20

It took me a while to come up with an idea though, if I have an idea in mind I usually can do my thing but the problem I often have (in life in general, not only with programming) is that I don't know where or how to start. So I'll have to work on that

2

u/otanan Jul 13 '20

Don't worry, I know very few people that know how to start, and often the few times they do it's only because they're already had that roadblock before with a very similar problem and have now learned how they can tackle it differently, and that will only come with practice (and rabbit holes). So don't ever feel bogged down because you aren't sure how to start just yet, keep working on it, not just with programming, but with life in general.

One benefit also of programming is the logic and problem solving skills that it teaches you, so doing these things with a computer will also teach you how to start in life!

2

u/shiny_arbok Jul 13 '20

Well that last part of your comment sounds great!

As long as I don't start answering my exam questions in programming language, that is... (right now I'm studying biomedical lab tech, but it isn't really for me, I'm thinking of changing to something else, maybe even programming, I'm testing the waters a bit)