Python 一段代码是';t工作|无错误信息
我正在做一个基本代数方程的计算器。我的代码有一部分不起作用,就是给出了错误的答案。我不确定问题到底是什么,但我知道它在Python 一段代码是';t工作|无错误信息,python,python-3.x,Python,Python 3.x,我正在做一个基本代数方程的计算器。我的代码有一部分不起作用,就是给出了错误的答案。我不确定问题到底是什么,但我知道它在if等式函数==(“减法”)行之后 对于一个测试问题,我正在输入280-x=287,因此答案应该是-7。我需要结束位来测试答案是否需要是否定的,但当它出现时,我得到的答案是7。当它在那里时,我根本没有得到任何回答 我期望输出为-7,但我根本没有得到任何答案。相反,我被送回了“你想做什么类型的函数?”的问题 import sys import math import random
if等式函数==(“减法”)
行之后
对于一个测试问题,我正在输入280-x=287
,因此答案应该是-7
。我需要结束位来测试答案是否需要是否定的,但当它出现时,我得到的答案是7。当它在那里时,我根本没有得到任何回答
我期望输出为-7,但我根本没有得到任何答案。相反,我被送回了“你想做什么类型的函数?”的问题
import sys
import math
import random
run = ("Yes")
while run == ("Yes"):
Function = (input("What type of function would you like to do? >"))
if Function == ("Equations"):
x = (input("What is your first number? >"))
y = (input("What is the answer to your equation? >"))
Equation_Function = (input("What is the function in your equation? >"))
if Equation_Function == ("Addition"):
Variable = float(y) - float(x)
print("The value of the variable is", Variable)
if Equation_Function == ("Subtraction"):
Variable = float(y) - float(x)
if float(Variable) + float(x) != float(y):
Variable = float(Variable) - float(Variable) - float(Variable)
print("The value of the variable is", Variable)
elif():
print("The value of the variable is", Variable)
import sys
import math
import random
while True:
Function = (input("What type of function would you like to do? >"))
if Function == ("Equations"):
x = (input("What is your first number? >"))
y = (input("What is the answer to your equation? >"))
Equation_Function = (input("What is the function in your equation? >"))
if Equation_Function == ("Addition"):
Variable = float(y) - float(x)
print("The value of the variable is", Variable)
if Equation_Function == ("Subtraction"):
Variable = float(x) - float(y)
if float(Variable) + float(x) != float(y):
Variable = float(Variable) - float(Variable) - float(Variable)
print("The value of the variable is", Variable)
else:
print("The value of the variable is", Variable)
我已经试过检查拼写和语法,因为这是我所知道的所有方法,也检查了任何可能的变量差异。应该是:
if Equation_Function == ("Subtraction"):
Variable = float(x) - float(y)
将elif()
替换为else
。你也可以写“while true”,但在功能上是一样的。我认为在实际工作之前,您需要处理几个边缘案例,但这些都超出了这个问题的范围
import sys
import math
import random
run = ("Yes")
while run == ("Yes"):
Function = (input("What type of function would you like to do? >"))
if Function == ("Equations"):
x = (input("What is your first number? >"))
y = (input("What is the answer to your equation? >"))
Equation_Function = (input("What is the function in your equation? >"))
if Equation_Function == ("Addition"):
Variable = float(y) - float(x)
print("The value of the variable is", Variable)
if Equation_Function == ("Subtraction"):
Variable = float(y) - float(x)
if float(Variable) + float(x) != float(y):
Variable = float(Variable) - float(Variable) - float(Variable)
print("The value of the variable is", Variable)
elif():
print("The value of the variable is", Variable)
import sys
import math
import random
while True:
Function = (input("What type of function would you like to do? >"))
if Function == ("Equations"):
x = (input("What is your first number? >"))
y = (input("What is the answer to your equation? >"))
Equation_Function = (input("What is the function in your equation? >"))
if Equation_Function == ("Addition"):
Variable = float(y) - float(x)
print("The value of the variable is", Variable)
if Equation_Function == ("Subtraction"):
Variable = float(x) - float(y)
if float(Variable) + float(x) != float(y):
Variable = float(Variable) - float(Variable) - float(Variable)
print("The value of the variable is", Variable)
else:
print("The value of the variable is", Variable)
要特别重写代码以通过“280-x=287”测试,我编辑如下:
import sys
import math
import random
while True:
Function = (input("What type of function would you like to do? >"))
if Function == ("Equations"):
x = (input("What is your first number? >"))
y = (input("What is the answer to your equation? >"))
Equation_Function = (input("What is the function in your equation? >"))
if Equation_Function == ("Addition"):
Variable = float(y) - float(x)
print("The value of the variable is", Variable)
if Equation_Function == ("Subtraction"):
Variable = float(x) + float(0 - float(y))
print("The value of the variable is", Variable)
它通过了测试,但我仍然警告您,这并不能代替深思熟虑地处理边缘情况。什么是
elif():
?你的意思是其他:
?我测试了一下,看起来@MichaelButscher的建议解决了这个问题。我仍然没有得到公式280-x=287的正确答案答案是x=-7,它应该告诉我它说7我发现我在第15行做的是“if float(Variable)+float(x)!”浮动(y)‘我需要做’如果浮动(变量)+浮动(y)!=float(x)'我仍然没有得到我的样本方程280-x=270的正确答案答案答案是x=-7@GreggMorgan这应该是正确的答案。你的算术运算错了。当你用上面的等式修正它时,你仍然需要修正下面的行,因为你在减法块中使用了加法运算。因此,var=x-y,这就是@WENWEN的答案。你不需要做进一步的操作,只需按原样扔掉计算出的变量就行了。这不管用,只得到了7个不正确的答案-7你说的@Charles是什么意思Landau@GreggMorgan我是说我编辑了我的答案,第二个代码块以-7通过了280-x=287
测试。很抱歉,我没有看到第二个代码块,因为我已经想到了这个问题,因此在我之前就有了解决方案(就像我刚刚发布这个:|)这确实有效,尽管我表示祝贺@查尔斯Landau@GreggMorgan如果您已经找到了解决方案,您应该A)将此答案或其他答案之一标记为正确答案,或者B)回答您自己的问题并将其标记为答案。