Python 如何将一个数字除以索引中的数字?
我的程序应该回答ax=b形式的方程Python 如何将一个数字除以索引中的数字?,python,python-3.x,Python,Python 3.x,我的程序应该回答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) 在第6行
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)
在第6行,我得到一个无效的语法错误。怎样才能回答这个方程呢?一个快速的解决方案。注意,您需要从string更改输入的类型(我使用float,但integer也可以) 变量的部分是什么?25摄氏度 答案是什么?八, 方程为25c=8 c=0.32
发生语法错误是因为python不知道如何处理“b/=number”
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)
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)
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)