[英]Cant do subtractions with negative numbers
print('=====CALCULADORA==========')
conta = str(input('')).strip()
operadores = ('+', '-', 'x', '/')
for o in range(0,4):
if operadores[o] in conta[1:len(conta)]:
if operadores[o] == '+':
print(f'{conta[0:conta.find("+")].strip()} + {conta[conta.find("+") + 1:].strip()} = {int(conta[0:conta.find("+")]) + int(conta[conta.find("+") + 1:])}')
if operadores[o] == '-':
print(f'{conta[0:conta.find("-")].strip()} - {conta[conta.find("-") + 1:].strip()} = {int(conta[0:conta.find("-")]) - int(conta[conta.find("-") + 1:])}')
Im creating a simple calculator code.我正在创建一个简单的计算器代码。
Doing additions works, but when I try to do operations like -4-6, the program dont work.加法有效,但是当我尝试执行 -4-6 之类的操作时,程序无法正常工作。 An error appears, saying: " ValueError: invalid literal for int() with base 10: '' "出现错误,说:“ ValueError:以 10 为底的 int() 的无效文字:''”
The code was supposed to do the expressions correctly, it parcially works, it seems the problem is when I repeat the operators.该代码应该正确地执行表达式,它特别有效,似乎问题出在我重复运算符时。 -4 + 3, or +4 - 3 actually works, but +4 + 3, or -4-3, dont. -4 + 3 或 +4 - 3 实际上有效,但 +4 + 3 或 -4-3 无效。
Take a close look at what your code does when you enter '-4-6'
.仔细查看输入'-4-6'
时代码的作用。
First the data entry:首先是数据输入:
conta = str(input('')).strip()
That works as expected, although this would do the exact same:这按预期工作,尽管这会做完全相同的事情:
conta = input().strip()
Then a defintion of operators, and your looping over it:然后是运算符的定义,然后循环遍历它:
operadores = ('+', '-', 'x', '/')
for o in range(0,4):
... # code using operadores[o]
This also works, although again it's more complicated than it needs to be:这也有效,尽管它再次比需要的更复杂:
operadores = '+-x/'
for o in operadores:
... # code using o as the current operator
Then the problematic part:然后是有问题的部分:
if operadores[o] in conta[1:len(conta)]:
You specifically skip the first character, probably to avoid detecting the -
at the start as the actual operator.您专门跳过第一个字符,可能是为了避免在开始时将-
检测为实际操作员。 However, the -
is still there in '-4-6'
.但是, -
仍然存在于'-4-6'
中。
Then, later:然后,稍后:
if operadores[o] == '-':
print(f'{conta[0:conta.find("-")].strip()} - {conta[conta.find("-") + 1:].strip()} = {int(conta[0:conta.find("-")]) - int(conta[conta.find("-") + 1:])}')
Here, you use conta.find("-")
to find the position of the operator, but of course here it actually finds the first -
, not the second.在这里,您使用conta.find("-")
来查找运算符的 position,但当然在这里它实际上找到了第一个-
,而不是第二个。
You could fix this code like this:您可以像这样修复此代码:
print('=====CALCULADORA==========')
conta = str(input('')).strip()
operadores = '+-x/'
for o in operadores:
if o in conta[1:len(conta)]:
if o == '+':
print(f'{conta[0:conta[1:].find("+")+1].strip()} + {conta[conta[1:].find("+")+2:].strip()} = {int(conta[0:conta[1:].find("+")+1].strip()) + int(conta[conta[1:].find("+")+2:].strip())}')
if o == '-':
print(f'{conta[0:conta[1:].find("-")+1].strip()} - {conta[conta[1:].find("-")+2:].strip()} = {int(conta[0:conta[1:].find("-")+1].strip()) - int(conta[conta[1:].find("-")+2:].strip())}')
However, that's still wildly complicated with a lot of reptition.然而,由于大量重复,这仍然非常复杂。 It also doesn't consider something like '4--2'
, which is subtracting -2
from 4
, and 6
should be the result.它也不考虑像'4--2'
这样的东西,它是从4
中减去-2
,结果应该是6
。 Never mind longer expressions, like '4+3+2'
.不要介意更长的表达式,例如'4+3+2'
。
It makes more sense to break up an input string into tokens and then perform the operations as they should be.将输入字符串分解为标记然后按应有的方式执行操作更有意义。 So, '-4+3+2'
would be broken up in ['-4', '+', '3', '+', '2']
and you can compute the result going over that.因此, '-4+3+2'
将分解为['-4', '+', '3', '+', '2']
并且您可以计算出遍历它的结果。
Of course this problem has been solved many times before, so there's good examples out there of even better strategies.当然,这个问题以前已经解决过很多次,所以有更好的策略的好例子。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.