我正在制作基本代数方程式的计算器。我的代码的一部分无法正常工作给出了错误的答案。我不确定是什么问题,但是我知道是在container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'space-between'
},
行之后。
对于测试题,我输入的是if Equation_Function == ("Subtraction")
,因此答案应该是280 - x = 287
。我需要结束符来测试答案是否需要否定的答案,但是当答案是否定的时,我得到的答案是7。当答案是否定的时,我根本没有答案。
我期望输出为-7,但我什么也没有得到。相反,我被发送回“您想做什么类型的功能?”问题。
-7
我已经尝试检查拼写和语法,因为这是我所知道的全部方法以及检查任何可能的变量差异。
答案 0 :(得分:0)
应为:
if Equation_Function == ("Subtraction"):
Variable = float(x) - float(y)
答案 1 :(得分:0)
将elif()
替换为else
。您也可以只写“ while true”,但功能上是相同的。我认为您需要先处理一些边缘情况,然后才能真正起作用,但这些问题超出了此问题的范围。
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)
它通过了测试,但我仍然提醒您,这不能替代深思熟虑地处理极端情况。