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

45

u/shiny_arbok Jul 13 '20

Thanks for the tip! The book I'm learning from, Automate the Boring Stuff, hasn't talked about f-strings yet, so I didn't know about them, but now that you told me, I'll try to make use of them in future programs :)

14

u/omarsika Jul 13 '20

He talks about it in the section on string formatting which im sure you'll reach quite. I'm currently taking the video course on udemy and he explains it really well!

13

u/shiny_arbok Jul 13 '20

Yeah I had a feeling that it would be discussed in a later chapter... Currently I'm on chapter 4, which teaches things about lists

That one's a bitch to learn tho in my opinion

1

u/tdubbau Jul 13 '20

A walk in the park next to the regular expressions chapter, IMO haha! :) longest 1.5 hours of video lessons (+ the book) I’ve ever watched, and I still don’t feel I “recall enough” to move forward yet.

5

u/black_daveth Jul 14 '20

move forward anyway, you don't need to memorise everything off by heart and nobody does anyway (unless its core to their day to day work of course).

most important thing to remember is what regular expressions are and how they might be able to make your life easier - just enough to be able to recognise good opportunities to use them - then when said opportunities arise you RTFM.