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

2

u/[deleted] Jul 13 '20

nice! now you could make a quadratic solver it just a few lines of code from here and you'll make a quadratic equation solver

2

u/shiny_arbok Jul 13 '20

I think I could do something like that... maybe a program that asks "do you need to calculate x or y?" after inputting the values for a, b, and c

If you say 'x', I can do an "if" statement where it follows this program, and if you say 'y', an "elif" statement where you enter the 'x' value and then it calculates y

Thanks for the idea, I'll definitely try it out!

2

u/[deleted] Jul 13 '20

you're welcome and you'd need a system of 2 equations containing x and y if you're gonna solve for x or y i think you know that and after making that you could learn tkinter and make an app that let's you visuallize vectors in 2d space and also visuallizes the addition and subtraction of vectors and takes the dot product and cross product and the determinant of 2 vectors that's what i made just a few weeks ago and im super proud of it and then you could move on to python scripts that can take the derivative of a function or solves an integral or even a differential equation solver or a partial differential equation solver or you could do something else different like making neural networks or making a web scraper whatever you know? just keep making stuff, keep making stuff while you're learning that's the best way to learn

3

u/shiny_arbok Jul 13 '20

Whoa, whoa, that sounds a bit too complicated for where I'm at yet, it'll take me some time to get there hahaha

Congratulations on making that app though!

2

u/[deleted] Jul 13 '20

thanks! If you already know the maths then you can probably do it after a few months of learning but all the best i hope you get amazing at coding also after a year of python learn c or c++ it's good to know a lower level language

2

u/shiny_arbok Jul 13 '20

I'm thinking of officially switching my studies to programming next year (because what I'm doing now isn't doing it for me), so chances are big I'll learn other programming languages too! Right now I'm just learning Python to test the waters a bit, to see if programming is something I'd actually like to do as a career.

And who knows, maybe I'll develop a groundbreaking app in a couple of years, that everyone will use in their daily lives

2

u/[deleted] Jul 13 '20

good for you man carry on never give up