Python can';无法找到工作代码和不工作代码之间的区别
这是我在这里的第二篇文章。 为什么总和计算器在一个版本的代码上运行,而在第二个版本上不运行 计划文件: 工作代码:Python can';无法找到工作代码和不工作代码之间的区别,python,Python,这是我在这里的第二篇文章。 为什么总和计算器在一个版本的代码上运行,而在第二个版本上不运行 计划文件: 工作代码: def sum_calc(lowerBound,higherBound,expression): calcLine = "" # storage of final expression # for all num in range (unless the last) add number, expression and sign '+
def sum_calc(lowerBound,higherBound,expression):
calcLine = "" # storage of final expression
# for all num in range (unless the last) add number, expression and sign '+' to the line
for num in range(int(lowerBound),int(higherBound)):
calcLine += "{}{} + ".format(str(num),expression)
# add last number with expression
calcLine += "{}{}".format(higherBound,expression)
print(eval(calcLine),"({})".format(calcLine)) # evaluate the resulting line
## main block
sum_calc(*input().split(" "))
不起作用的版本:(我试图把它改写成一个挑战)
不工作版本显示异常EOF错误
在代码中,代码下面
calcLine+=“{}{}+.”格式(更高,表达式)
正在将etra+
添加到表达式中。iecalcLine=1*2+2*2+3*2+
现在使用额外的+
在此表达式上运行eval
,将产生错误
从表达式中删除+
,并将代码设置为
calcLine+=“{}{}”。格式(更高,表达式)
它的语法错误。
替换
calcLine+=“{}{}+.”格式(更高,表达式)
withcalcLine+=“{}{}.”格式(更高,表达式)
。代码现在应该可以正常工作。当建议的更改是providen时,此代码可以工作
def summCalc (lower, higher, expression):
calcLine = "" # storage of final expression
for num in range (int(lower),int(higher)):
calcLine += "{}{} + ".format(str(num), expression)
calcLine += "{}{} ".format(higher, expression)
print(eval(calcLine),"({})".format(calcLine)) # evaluate the resulting line
summCalc(*input().split(" "))
您正在运行的代码
eval()
没有显示给我们,但该代码有错误。变量和函数名应遵循带有下划线的小写形式。
def summCalc (lower, higher, expression):
calcLine = "" # storage of final expression
for num in range (int(lower),int(higher)):
calcLine += "{}{} + ".format(str(num), expression)
calcLine += "{}{} + ".format(higher, expression)
print(eval(calcLine),"({})".format(calcLine)) # evaluate the resulting line
summCalc(*input().split(" "))
def summCalc (lower, higher, expression):
calcLine = "" # storage of final expression
for num in range (int(lower),int(higher)):
calcLine += "{}{} + ".format(str(num), expression)
calcLine += "{}{} ".format(higher, expression)
print(eval(calcLine),"({})".format(calcLine)) # evaluate the resulting line
summCalc(*input().split(" "))