user12393340
user12393340

Reputation: 13

How do you divide a number by a number from a index?

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?

Upvotes: 1

Views: 47

Answers (3)

Ahmed Soliman
Ahmed Soliman

Reputation: 1710

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)

Upvotes: 0

Eshonai
Eshonai

Reputation: 121

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

Upvotes: 1

Kris
Kris

Reputation: 420

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)

Upvotes: 1

Related Questions