简体   繁体   中英

How do I print a square root to the console in python?

I am trying to write code that can print a square root to the console. Here is an example of some code

from math import sqrt

print("ax\N{SUPERSCRIPT TWO} + bx + c")

a = int(input("Enter a: "))
b = int(input("Enter b: "))
c = int(input("Enter c: "))

x1 = (-b - sqrt(b ** 2 - 4*a*c))/(2*a)
x2 = (-b + sqrt(b ** 2 - 4*a*c))/(2*a)

x3 = f"{-b} \N{PLUS-MINUS SIGN} \N{SQUARE ROOT}{b ** 2 - 4*a*c}/{2*a}"

print(f"{a if a != 1 else ''}x\N{SUPERSCRIPT TWO} {'+' if b > 0 else '-'} {b*-1 if b < 0 else b}x {'+' if c > 0 else '-'} {c*-1 if c < 0 else c}")
print()
print(f"x = {x1}, x = {x2}")
print()
print(f"x = {x3}")

The above code just calculates the quadratic formula on given input. I'm not satisfied with the console output styling.

Right now the output look like this x = 9 ± √101/2

I was wondering if there was a way (maybe with some extra module like rich?) for me to make an overline for the number

You can use sympy module for display and calculation.

https://docs.sympy.org/latest/index.html

For display purpose:

from sympy import *

a,b,c = symbols('a b c', Positive = True, Real = True)
x1 = symbols('\Delta_t', Real = True)

x1 = (-b + sqrt(b ** 2 - 4*a*c))/(2*a)
x1

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM