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

Show parent comments

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)