[英]Python Code not working correctly
Hi this is my second post on this code I dont get errors but the if else statement is not working how I want the if(calculate[2] == opperations: code is not working, it wont register even if i have a - or + there. Any ideas why? It gives me the response Sorry invalid text3.嗨,这是我关于此代码的第二篇文章,我没有收到错误,但 if else 语句无法正常工作,我希望 if(calculate[2] == opperations: 代码不起作用,即使我有 - 或+那里。有什么想法为什么?它给了我回复抱歉无效的文本3。
calculate = input("Enter the problem in the format x + y = z ")
opperations = ["+", "-", "*", "/"]
numbers = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
space = " "
a = calculate[0]
a = int(a)
b = calculate[4]
b = int(b)
def opperation():
if opperations == "+":
A = 1
elif opperations == "-":
A = 2
elif opperations == "*":
A = 3
elif opperations == "/":
A = 4
if calculate[0] in numbers:
if len(calculate) > 1:
if calculate[2] == opperations:
if calculate[1] == space:
if A == 1:
c = a + b
print (c)
elif A == 2:
c = a - b
print (c)
elif A == 3:
c = a * b
print(c)
elif A == 4:
c = a / b
print(c)
else:
print("Sorry invalid text5")
else:
print("Sorry invalid text4")
else:
print("Sorry invalid text3")
else:
print("Sorry invalid text2")
else:
print("Sorry invalid text1")
The condition 条件
if calculate[2] == opperations:
will compare calculate[2]
with the whole list ["+", "-", "*", "/"]
. 将
calculate[2]
与整个列表["+", "-", "*", "/"]
。 You can instead use the in
keyword, to check if a element is inside a sequence: 您可以改为使用
in
关键字来检查元素是否在序列中:
if calculate[2] in opperations:
# ...
Also , in the part where you compare opperations
with "+"
, "-
... You should be comparing them with calculate[2]
: 此外 ,在您将
opperations
与"+"
进行比较的部分中, "-
......您应该将它们与calculate[2]
进行比较:
if calculate[2] == "+":
A = 1
elif calculate[2] == "-":
A = 2
elif calculate[2] == "*":
A = 3
elif calculate[2] == "/":
A = 4
Notes: Your approach will just handle operations with 1
digit numbers. 注意:您的方法只处理
1
位数的操作。 I would recommend you to use the split(" ")
method so you can use it like: 我建议你使用
split(" ")
方法,这样你就可以使用它:
s = "100 + 20"
parts = s.split(" ") # split by space
parts[0] # 100
parts[1] # +
parts[2] # 20
In addition to Christian's suggestions, the if
elif
chain could be replaced by a dictionary lookup. 除了Christian的建议之外,
if
elif
链可以被字典查找替换。 In this example lambda a, b: a+b
is a function that takes two values a
and b
and return there sum. 在这个例子中,
lambda a, b: a+b
是一个函数,它接受两个值a
和b
并返回sum。 That means opperations["+"]
returns a function and opperations["+"](a,b)
returns the sum of a
and b
. 这意味着
opperations["+"]
返回一个函数, opperations["+"](a,b)
返回a
和b
的总和。
opperations = { "+": lambda a, b: a+b,
"-": lambda a, b: a-b,
"*": lambda a, b: a*b,
"/": lambda a, b: a/b }
calculate = raw_input("Enter the problem in the format x + y = z ")
numbers = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
space = " "
a = calculate[0]
a = int(a)
b = calculate[4]
b = int(b)
if calculate[0] in numbers:
if len(calculate) > 1:
if calculate[2] in opperations:
if calculate[1] == space:
c = opperations[calculate[2]](a,b)
print (c)
else:
print("Sorry invalid text4")
else:
print("Sorry invalid text3")
else:
print("Sorry invalid text2")
else:
print("Sorry invalid text1")
I think it would be easier to make a new python file and then create a function in it and then import it:我认为创建一个新的python文件然后在其中创建一个函数然后导入它会更容易:
def calculate(calculation):
with open('calculation_file', 'w') as FILE:
FILE.write(
'''
def calculate():
return ''' + calculation + '''
''')
import calculation_file as c
return c.calculate
it creates a file and then imports it and returns what the calculation_file There are probably some bugs though because I have not run it yet
also you probably need to make the variables a
, b
an intager with int(a)
它创建一个文件,然后导入它并返回计算文件的内容
There are probably some bugs though because I have not run it yet
您可能还需要使用int(a)
使变量a
, b
成为一个int(a)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.