Showing input prompt in python - python

I am using IDLE for Python on a Mac OS. I wrote the following in a .py file:
import math
def main():
print "This program finds the real solution to a quadratic"
print
a, b, c = input("Please enter the coefficients (a, b, c): ")
discRoot = math.sqrt(b * b-4 * a * c)
root1 = (-b + discRoot) / (2 * a)
root2 = (-b - discRoot) / (2 * a)
print
print "The solutions are: ", root1, root2
main()
IDLE now permanently displays:
This program finds the real solution to a quadratic
Please enter the coefficients (a, b, c):
When I enter 3 numbers (ex: 1,2,3) IDLE does nothing. When I hit enter IDLE crashes (no crash report).
I quit and restarted but IDLE is now permanemtly displaying the above and won't respond to other files.

There is no real solution to the equation X^2 + 2x + 3 = 0. You will get a ValueError when trying to take the square root of b * b-4 * a * c, which is negative. You should handle this error case somehow. For example, a try/except:
import math
def main():
print "This program finds the real solution to a quadratic"
print
a, b, c = input("Please enter the coefficients (a, b, c): ")
try:
discRoot = math.sqrt(b * b-4 * a * c)
except ValueError:
print "there is no real solution."
return
root1 = (-b + discRoot) / (2 * a)
root2 = (-b - discRoot) / (2 * a)
print
print "The solutions are: ", root1, root2
main()
Or you can detect that the discriminant is negative ahead of time:
import math
def main():
print "This program finds the real solution to a quadratic"
print
a, b, c = input("Please enter the coefficients (a, b, c): ")
discriminant = b * b-4 * a * c
if discriminant < 0:
print "there is no real solution."
return
discRoot = math.sqrt(discriminant)
root1 = (-b + discRoot) / (2 * a)
root2 = (-b - discRoot) / (2 * a)
print
print "The solutions are: ", root1, root2
main()
Result:
This program finds the real solution to a quadratic
Please enter the coefficients (a, b, c): 1,2,3
there is no real solution.

The math module does not support complex numbers. If you replace import math with import cmath and math.sqrt with cmath.sqrt, your script should work like a charm.
EDIT: I just read "This program finds the real solution to a quadratic". Taking into consideration that you only want real roots, you should check for negative discriminants as the Kevin has pointed out.

The reason I see for your program failing is this:
a, b, c = 1, 2, 3
num = b * b - 4 * a * c
print num
It comes out as -8.
You normally can't have a negative number within a square root.
Like what the person above me says, import cmath should work.
http://mail.python.org/pipermail/tutor/2005-July/039461.html
import cmath
a, b, c = 1, 2, 3
num = cmath.sqrt(b * b - 4 * a * c)
print num
= 2.82842712475j

Related

How to get variable values from a txt file with a specified format?

I am writing a program that solves quadratic equations, with the variables assigned from a .txt file, with this specific format:
The text file will contain lines of comma separated text – each line contains three floating point numbers. The first number is the value for A, the second number is the value for B, and the third number is the value for C.
Now I have my code mostly written out I am just unsure how I would assign these variables from a .txt file. Here is my current code:
print("\nWelcome to the Blockhouse Bay College Quadratic Equation Program")
file = input("\nPlease input text file name: ")
file = file + ".txt"
text_file = open(file, "r")
import math
# function for finding roots
def equationroots( a, b, c):
# calculating discriminant using formula
dis = b * b - 4 * a * c
sqrt_val = math.sqrt(abs(dis))
# checking condition for discriminant
if dis > 0:
print("There are two distinct roots ")
print((-b + sqrt_val)/(2 * a))
print((-b - sqrt_val)/(2 * a))
elif dis == 0:
print("There is one real root")
print(-b / (2 * a))
# when discriminant is less than 0
else:
print("Complex Roots")
print(- b / (2 * a), " + i", sqrt_val)
print(- b / (2 * a), " - i", sqrt_val)
# Driver Program
#<---------------Need these variables assigned to lines from .txt file
a = 1
b = 10
c = -24
# If a is 0, then incorrect equation
if a == 0:
print("Cannot be a quadratic equation if a is zero")
else:
equationroots(a, b, c)
Note: Please note at the start of my code I have asked the user to input the file name, I still need to work on this as I need to create an error message for when the file does not exist. Just ignore this as I can fix that later.
with open('file.txt') as f:
content = f.read()
for line in content.splitlines():
a, b, c = map(float, content.split(','))
Complete program:
print("\nWelcome to the Blockhouse Bay College Quadratic Equation Program")
import math
# function for finding roots
def equationroots( a, b, c):
# calculating discriminant using formula
dis = b * b - 4 * a * c
sqrt_val = math.sqrt(abs(dis))
# checking condition for discriminant
if dis > 0:
print("There are two distinct roots ")
print((-b + sqrt_val)/(2 * a))
print((-b - sqrt_val)/(2 * a))
elif dis == 0:
print("There is one real root")
print(-b / (2 * a))
# when discriminant is less than 0
else:
print("Complex Roots")
print(- b / (2 * a), " + i", sqrt_val)
print(- b / (2 * a), " - i", sqrt_val)
filename = input("\nPlease input text file name: ")
with open(filename + '.txt') as f:
content = f.read()
for line in content.splitlines():
a, b, c = map(float, content.split(','))
# If a is 0, then incorrect equation
if a == 0:
print("Cannot be a quadratic equation if a is zero")
else:
equationroots(a, b, c)
Try this:
I had to move some of your code around, but I left inline comments to explain what I changed. I also included a check to make sure the path exists.
import math # Python convention is to put imports at the top
import os
print("\nWelcome to the Blockhouse Bay College Quadratic Equation Program")
# function for finding roots
def equationroots( a, b, c):
# calculating discriminant using formula
dis = b * b - 4 * a * c
sqrt_val = math.sqrt(abs(dis))
# checking condition for discriminant
if dis > 0:
print("There are two distinct roots ")
print((-b + sqrt_val)/(2 * a))
print((-b - sqrt_val)/(2 * a))
elif dis == 0:
print("There is one real root")
print(-b / (2 * a))
# when discriminant is less than 0
else:
print("Complex Roots")
print(- b / (2 * a), " + i", sqrt_val)
print(- b / (2 * a), " - i", sqrt_val)
file = input("\nPlease input text file name: ")
file = file + ".txt"
if os.path.exists(file): # check if file exists
with open(file, "rt") as text_file: # using a with statement is
# preferred over directly
# opening the file
for line in text_file: # iterate through lines of file
# the next line splits the `line` by "," commas,
# removes whitespace and casts each item to float
a,b,c = [float(i.strip()) for i in line.split(',')]
if a == 0:
print("Cannot be a quadratic equation if a is zero")
else:
equationroots(a, b, c)
else:
# print some message
you could read the file like this:
f = open(file_name,"r")
for line in f.readlines():
a,b,c = map(float,line.split(","))
# now call your function
if a == 0:
print("Cannot be a quadratic equation if a is zero")
else:
equationroots(a, b, c)
we started by opening the file then we iterate over the lines using file.readlines() then for every line we splitted the values and converted all of them to a float using map()

Is there a way to just print the 'y' when your answer is (x, y)?

Is there a way that i can modify this, so my answer only comes out as the 'y' coordinate?? The output I'm looking for is for example 'Triangle Number: 55385'. I'm pretty new to coding, so I don't really understand how to do that. (Python 2.7)
from sympy.solvers import solve
from sympy import Symbol
t = Symbol('t')
e = Symbol('e')
x = Symbol('x')
print "Triangle Number:", solve(t * (t + 1) / 2 - a, t)
print "Pentagonal Number:", solve((e * (3 * (e) - 1) / 2) - a, e)
print "Hexagonal Number:", solve((x * (2 * (x) - 1)) - a, x)
#OUTPUT:
Triangle Number: [-55386, 55385]
Pentagonal Number: [-95930/3, 31977]
Hexagonal Number: [-55385/2, 27693]
Yes, you can request the last entry in the list returned to be printed, instead of the entire sequence:
print "Triangle Number:", solve(t * (t + 1) / 2 - a, t)[-1]
print "Pentagonal Number:", solve((e * (3 * (e) - 1) / 2) - a, e)[-1]
print "Hexagonal Number:", solve((x * (2 * (x) - 1)) - a, x)[-1]
#OUTPUT IS BELOW
Triangle Number: 55385
Pentagonal Number: 31977
Hexagonal Number: 27693
Something you can do to have solve only return the values that fit your criteria for e, x and t is to define them as nonnegative values. Doing this, solve should only return a list with the values for which you may be looking:
>>> e, x, t = symbols('e x t', nonnegative=True, integer=True)
>>> a = 1533776805
>>> solve(t * (t + 1) / 2 - a, t)
[55385]

Python Print statements don't work

a = input('enter a ')
b = input('enter b ')
c = input('enter c ')
def is_right_angled(a, b, c):
a, b, c = sorted([a, b, c]) #sort inputs smallest to largest
pathag=(a * a + b * b - c * c) #< 0.1 #a ^2 + b ^2 - c ^2 should = 0 approx
if pathag<0.1: # test "pathag" to to see if close
print ("This is a right triangle")
else: # if "pathag" not close, not "right"
print ("This is NOT a right triangle")
return abs(a * a + b * b - c * c) < 0.1
There could be a couple issues specific to the print function not working (I think you might also want to revisit some of the logical assumptions driving your is_right_angled function.)
1) An input function creates a string variable. You will need to explicitly convert this to an int or float variable in order for your function to correctly work with these variables.
a = float(input('enter a '))
2) You are not actually calling the function in the above code. Be sure to call it or the code won't run. Add this to the end of your script:
is_right_angled(a, b, c)

python expected an indented block

i don't know why that appears when i try to run. i tried to fix but i can't. is there any errors?
import math
print(" Hello....\n This program will ask you to enter 3 numbers \n (a, b and c) that will be used in the quadratic formula to \n calculate the roots of an equation ")
a=float(input("Enter a Value for a: "))
while(a <= 0):
print(" Please enter a number that is larger than 0.") <br/>
a=float(input("Please enter a value for A: "))
b=float(input("Enter a Value for b: "))
while(b <= a): <br/>
print(" Please enter a value that is larger than A.") <br/>
b=float(input("Enter a value for B: "))
c=float(input("Enter a Value for C: "))
root1 = float(-b + math.sqrt(b * b - 4 * a * c)/(2 * a)) <br/>
root2 = float(-b - math.sqrt(b * b - 4 * a * c)/(2 * a))
print("Root 1 is " , root1)<br/>
print("Root 2 is " , root2)<br/>
input("prompt: ")
First: In Python, indenting is a must. It is a syntactically feature to have nested code indented instead of spamming the source with paranthesis'.
Second: Obviously, you copy pasted HTML code from somewhere. Get rid of <br />.

Why does math.sqrt result in ValueError: math domain error?

What causes the problem?
from math import sqrt
print "a : "
a = float(raw_input())
print "b : "
b = float(raw_input())
print "c : "
c = float(raw_input())
d = (a + b + c)/2
s = sqrt(d*(d-a)*(d-b)*(d-c))
print("a+b+c =", a, b, c)
print("Distr. =", d*2, "Area =", s)
Error:
Traceback (most recent call last):
File "C:/Python27/fájlok/háromszög terület2.py", line 11, in <module>
s = sqrt(d*(d-a)*(d-b)*(d-c))
ValueError: math domain error
See also: Why does math.log result in ValueError: math domain error? for the equivalent problem using math.log; Python math domain error using math.acos function for the equivalent problem using math.acos.
The problem is that the Heron's formula holds good only when the sum of the two numbers are greater than the third. You need to check that explicitly.
A better way as you are using a code to do that is by using Exception handling
try:
s = sqrt(d*(d-a)*(d-b)*(d-c))
print "a+b+c =", a, b, c
print "Distr. =", d*2, "Area =", s
except ValueError:
print "Please enter 3 valid sides"
If you want to do it without try block you can do it as
delta = (d*(d-a)*(d-b)*(d-c))
if delta>0:
s = sqrt(delta)
print "a+b+c =", a, b, c
print "Distr. =", d*2, "Area =", s
else:
print "Please enter 3 valid sides"
sqrt gives that error when you try to use it with a negative number. sqrt(-4) gives that error because the result is a complex number.
For that, you need cmath:
>>> from cmath import sqrt
>>> sqrt(-4)
2j
>>> sqrt(4)
(2+0j)
I got the same error with my code until I used cmath instead of math like aneroid said:
import sys
import random
import cmath
x = random.randint(1, 100)
y = random.randint(1, 100)
a = 2 * x * cmath.sqrt(1 - x * 2 - y * 2)
b = 2 * cmath.sqrt(1 - x * 2 - y * 2)
c = 1 - 2 * (x * 2 + y * 2)
print ( 'The point on the sphere is: ', (a, b, c) )
This way ran my code properly.
Use cmath instead..
import cmath
num=cmath.sqrt(your_number)
print(num)
Now regardless of whether the number is negetive or positive you will get a result...

Categories

Resources