09. Quadratic Formula (+15 XP)


# User Input


Thus far, we've only been outputting things to the user, which makes our programs very one-sided and not that fun. Almost every popular website, mobile app, or video game today has both input and output. So how do we get input from the user?

Python uses the input() function to get user input.

          
          username = input("Enter your name: ")

          print(username)
          
        

The output will say "Enter your name: " and the user can type in something, hit enter, and whatever the user typed gets stored into the variable username.

So here, suppose the user type in their own name and press enter, it will output their name.

The whole process looks like this:



By default, the user input is stored as a text string, which is working great for now.

But what about when we want to get a number from a user?

In that case, we would need to wrap an int() around the input() to convert the text string into a number:

          
          age = int(input("What's your age:"))

          print(age)
          
        

Now that the user types 24 and press enter, the age variable will be an integer 24, and not a text string "24".


# Instructions


If you slept through your algebra class, a quadratic equation is an equation that has the form:

\[ax² + bx + c = 0\]

The \(x\) represents an unknown, and \(a\), \(b\), \(c\) represent known numbers.

Create a quadratic.py program that solves the quadratic equation by finding the two \(x\)'s using the quadratic formula:

\[x = {-b \pm \sqrt{b^2-4ac} \over 2a}\]

Your program should ask the user for three different numbers (\(a\), \(b\), \(c\)), do some calculations on them, and spit out the two \(x\)'s.


Back

Solution: quadratic.py