My program is supposed to answer equations in the form ax = b
a = input("What is the part with the variable? ")
b = input("What is the answer? ")
print('the equation is', a , '=', b)
letter = a[-1]
number = a[0:-1]
answer = b /= number
print(letter,"=",answer)
In line 6, I'm getting an invalid syntax error. How can do it to answer the equation?
a = input("What is the part with the variable? ")
b = input("What is the answer? ")
print('the equation is', a , '=', b)
letter = a[-1]
number = a[0:-1]
answer =float(b)/float(number)
print(letter,"=",answer)
What is the part with the variable? 25c
What is the answer? 8
the equation is 25c = 8
c = 0.32
A quick solution. Note, you need to change the type of your input from string (I used float for this, but integer should also work).
a = float(input("What is the part with the variable? "))
b = float(input("What is the answer? "))
print('the equation is',a,'* X =',b)
# solve the equation: aX = b for X
x = b/a
print("X =",x)
This is a better version of you code :
a = input("What is the part with the variable? ")
b = input("What is the left hand side of the equation? ")
print('the equation is {}x = {}'.format(a , b))
answer = float(b) /float(a) // convert the inputs to floats so it can accept mathematical operations
print("x=",answer)
Related
This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 1 year ago.
Hello I'm making a random program and right now I have it asking a user if they want to add, subtract, multiply, or divide and that's going well but the if statement wont go through all the options. Whatever is in the first of the nested if statement is what the function preforms.
Here's the code:
#Test2.py
import random
x = input("Hello what is the issue? ").capitalize()
if x == "Can't decide what to do":
y = input("How many choices are there? (please type as numbers in a row) ")
yl = list(y)
print("Number ", random.choice(yl) ," is your choice.")
elif x == "I need help with math":
m = input("Okay, are you adding, subtracting, multiplying, dividing, or something else? ").capitalize()
if m == "Adding" or "Add" or "Addition":
q = input("Okay what is the first number? ")
w = input("What is the second number? ")
r = input("Enter the third number: (if there isnt another number please enter 0)")
t = input("Enter the fourth number: (if there isnt another number please enter 0) ")
u = input("Enter the fifth number: (if there isnt another number please enter 0) ")
m1 = int(q)+int(w)+int(r)+int(t)+int(u)
print("The answer is: ", m1)
elif m == "Subtracting" or "Subtract" or "Subtraction":
i = input("Okay what is the first number? ")
o = input("What is the second number? ")
p = input("Enter the third number: (if there isnt another number please enter 0) ")
a = input("Enter the fourth number: (if there isnt another number please enter 0) ")
s = input("Enter the fifth number: (if there isnt another number please enter 0) ")
m2 = float(i) - float(o) - float(p) - float(a) - float(s)
print("The answer is: ", m2)
elif m == "Multiplying" or "Multiply" or "Multiplication":
d = input("Okay what is the first number? ")
f = input("What is the second number? ")
g = input("Enter the third number: (if there isnt another number please enter 1) ")
h = input("Enter the fourth number: (if there isnt another number please enter 1) ")
j = input("Enter the fifth number: (if there isnt another number please enter 1) ")
m3 = float(d) * float(f) * float(g) * float(h) * float(j)
print("The answer is: ", m3)
elif m == "Dividing" or "Divide" or "Division":
k = input("Okay what is the first number? ")
l = input("What is the second number? ")
z = input("Enter the third number: (if there isnt another number please enter 1) ")
x = input("Enter the fourth number: (if there isnt another number please enter 1) ")
c = input("Enter the fifth number: (if there isnt another number please enter 1) ")
m4 = float(k)%float(l)%float(z)%float(x)%float(c)
print("The answer is: ", m4)
else:
quit
Thank you for any and all help, have a great day!
You need to put "m ==" after every "or". Python doesn't know what to compare to, if you don't give it the variable to compare to after every "or" or "and" logic operator.
For example:
elif m == "Subtracting" or m == "Subtract" or m == "Subtraction":
I am experimenting with classes for the first time, and I wanted to create a program that asks the user for an input a,b, and c, and then solves for x for the equation forms stated in the print statements. However, I am having issues with the class, giving me an error that I am not using the variables in the class, missing the 5 positional arguments. Any help would be amazing, thanks so much.
class EquationSolver:
def MonomialSolver(self,a,b,c,x):
a = input("Enter Input for a:")
b = input("Enter Input for b:")
c = input("Enter input for c:")
x = (c+b)/a
print("For the equation in the format ax-b=c, with your values chosen x must equal", x)
def PolynomialSolver(self,a,b,c,x):
a = input("Enter Input for a:")
b = input("Enter Input for b:")
c = input("Enter input for c:")
x = (c^2 + b) / a
print("For the equation in the format sqrt(ax+b) = c, with your values chosen x must equal", x)
MonomialSolver()
PolynomialSolver()
The problem I see is the inputs for the functions. You do not need the self parameter or any other parameter. The functions should run outside the loop. The edited version should loop something like this:
class EquationSolver:
def MonomialSolver():
# Uses float() to turn input to a number
a = float(input("Enter Input for a:"))
b = float(input("Enter Input for b:"))
c = float(input("Enter input for c:"))
x = (c+b)/a
print("For the equation in the format ax-b=c, with your values chosen x must equal", x)
def PolynomialSolver():
a = float(input("Enter Input for a:"))
b = float(input("Enter Input for b:"))
c = float(input("Enter input for c:"))
x = (c^2 + b) / a
print("For the equation in the format sqrt(ax+b) = c, with your values chosen x must equal", x)
EquationSolver.MonomialSolver()
EquationSolver.PolynomialSolver()
I get the error "TypeError: can't multiply sequence by non-int of type 'str'" in line 5 which is the "number = str(c(c*r)**x)" this is a weak attempt at making a growth calculator. Any help would be appriciated I am fairly new
import math
c = raw_input("what is the intial number?")
r = raw_input("What is the rate of growth?")
x = raw_input("How many years are taking place?")
int(c)
int(r)
int(x)
number = str(c(c*r)**x)
print (number)
You forgot to reassign the variables:
import math
c = raw_input("what is the intial number?")
r = raw_input("What is the rate of growth?")
x = raw_input("How many years are taking place?")
c = int(c)
r = int(r)
x = int(x)
number = str(c*(c*r)**x)
print (number)
The better way to do in the same statement only, like this (user friendly way).
import math
c = int(raw_input("what is the intial number?\n=> "))
r = int(raw_input("What is the rate of growth?\n=> "))
x = int(raw_input("How many years are taking place?\n=> "))
number = str(c*(c*r)**x)
print(number)
Output
what is the intial number?
=> 3
What is the rate of growth?
=> 4
How many years are taking place?
=> 5
746496
int() returns the parsed integer. You also need to put * for each multiplication operation you're doing.
print can print integers by default, no need for the str around number
import math
c = raw_input("what is the intial number?")
r = raw_input("What is the rate of growth?")
x = raw_input("How many years are taking place?")
c = int(c)
r = int(r)
x = int(x)
number = (c * (c * r)) ** x
print(number)
I'm new to python and attempting to write a linear equation using Cramer's Rule. I've entered the formula and have code prompting user to enter a,b,c,d,e and f but my code is getting a syntax error. I'd like to fix the code and also have a system for researching and correcting future errors.
a,b,c,d,e,f = float(input("Enter amount: ")
a*x + by = e
cx + dy = f
x = ed-bf/ad-bc
y=af-ed/ad-bc
if (ad - bc == 0)print("The equation has no solution")
else print ("x=" x, "y=" y,)
Basically, your code was one giant syntax error. Please read some basic tutorial, as was suggested in the comments. Hopefully this will help in the learning process (I didn't go through the actual math formulas):
a = float(input("Enter a: "))
b = float(input("Enter b: "))
c = float(input("Enter c: "))
d = float(input("Enter d: "))
e = float(input("Enter e: "))
f = float(input("Enter f: "))
##a*x + by = e
##cx + dy = f
if (a*d - b*c == 0):
print("The equation has no solution")
else:
x = (e*d-b*f)/(a*d-b*c)
y = (a*f-e*d)/(a*d-b*c)
print ("x=%s" % x, "y=%s" % y)
You have to put * between the numbers you want to multiply. You had one parenthesis missing from your input statement. The equations themselves are commented out, because otherwise Python takes them as a code (incorrectly written one). You have to enclose the denominator in parentheses because math. You have to end the if, else, elif, for, while and such with :. Indentation is VERY important in Python.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
Python noob here. I'm trying to add a set of input variables defined in an 'if' statement and whenever I try to find the sum it will just display the values inline. For example, when a, b, c, and d equal 5, perimeter = 555...
shape = raw_input("Is the plot a (q) quadrilateral or (t) triangle?")
if shape.lower() == "q":
a = raw_input("What is the length in feet of side 'a'?")
b = raw_input("What is the length in feet of side 'b'?")
c = raw_input("What is the length in feet of side 'c'?")
d = raw_input("What is the length in feet of side 'd'?")
elif shape.lower() == "t":
a = raw_input("What is the length in feet of side 'a'?")
b = raw_input("What is the length in feet of side 'b'?")
c = raw_input("What is the length in feet of side 'c'?")
else:
print "Please enter 'q' for quadrilateral or 't' for triangle."
if shape.lower() == "q":
perimeter = a + b + c + d
elif shape.lower() == "t":
perimeter = a + b + c
else:
print "Please make sure you enter numbers only."
print perimeter
str values can be added to each other much like numbers. The + operator you use works fine, but concatenates values for strings. The result of raw_input is a string (str), so that's why you'd see '555' in stead of 15. To sum numbers, use int() to coerce the values to numbers before adding them up:
try:
a = int(raw_input('gimme a number'))
except ValueError as e
print 'that was not a number, son'
Make sure that your raw_input actually Is an int():
shape = raw_input("Is the plot a (q) quadrilateral or (t) triangle?")
if shape.lower() == "q":
try:
a = raw_input("What is the length in feet of side 'a'?")
b = raw_input("What is the length in feet of side 'b'?")
c = raw_input("What is the length in feet of side 'c'?")
d = raw_input("What is the length in feet of side 'd'?")
perimeter = int(a) + int(b) + int(c) + int(d)
except ErrorValue as e
print "Please make sure you enter numbers only."
elif shape.lower() == "t":
try:
a = raw_input("What is the length in feet of side 'a'?")
b = raw_input("What is the length in feet of side 'b'?")
c = raw_input("What is the length in feet of side '
perimeter = int(a) + int(b) + int(c)
except ErrorValue as e
print "Please make sure you enter numbers only."
else:
print "Please enter 'q' for quadrilateral or 't' for triangle."
for variables a, b, c, and d, use input(prompt) instead of raw_input(prompt).
raw_input returns a string,
but input returns the console input evaluated as a python literal. (As of right now, you're
concatenating strings, not adding integers).
Your code is not a good design. What if you want to add more shapes, hexagon, octagon and so on. You can actually use a dict to store shape mapping to number of sides. You don't have to write multiple if statements for each shape. You will have to do less type checking and you could use python builtin function sum to return the parameter. Go on now and try the following:
d = {'q': 4, 't': 3}
shape = raw_input("Is the plot a (q) quadrilateral or (t) triangle?\n")
if shape.lower() not in d:
print "Please enter 'q' for quadrilateral or 't' for triangle."
else:
sides = []
for i in range(0,d.get(shape.lower())):
side = raw_input("What is the length in feet of side " + str(i+1))
try:
sides.append(int(side))
except ValueError:
print "Integer value only"
print sum(sides)
I did this using a dictionary.
sides = {'a':0,'b': 0,'c': 0,'d': 0}
perimeter = 0
shape = raw_input("Is the plot a (q) quadrilatral or (t) triangle?: ")
if shape.lower() == "q":
for side, length in sides.iteritems():
sides[side] = input("What is the length (in feet) of side %s?: " % side)
perimeter+=int(sides[side])
elif shape.lower() == "t":
sides.pop("d",None)
for side, length in sides.iteritems():
sides[side] = input("What is the length (in feet) of side %s?: " % side)
perimeter+=int(sides[side])
else:
print "Please enter 'q' or 't'."
print "Perimeter is: %d" % perimeter
I figured a dictionary would be easier to use. Might be much cleaner, rather than repeat yourself.