如何减去传递给函数的动态值 [英] How can I subtract dynamic values passed to a function
本文介绍了如何减去传递给函数的动态值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我发现很难存储第一个值,它是通过函数动态传递的。
我试过用下面的代码,但是显示的第一个值将存储-3(期望值3),因为逻辑减法 - = i将计算0-第一个值,这将是负数。请帮助我按照正确的流程进行操作。
同样,分工逻辑
谢谢。
我尝试过:
减法= 0
def sub(* x):
l2 = list(x)
l2.sort(reverse = True )
sortedValue = l2
for i in sortedValue:
< span class =code-keyword> global 减法
减法 - = i # 第一个参数不应该改变。但是这里的值变为负数
print ( 减去数字{0} = .format(sortedValue),减法)
return 减法
SubtractionResult = sub( 20 , 2 , 5 )
解决方案
减法= 0
def sub(* x):
l2 = list(x)
l2.sort(reverse = True)
sortedValue = l2
for i in sortedValue:
global 减法
你有两种不同的拼写:减法
和减法
I am finding it difficult to store the first value, which is dynamically passed through a function.
I tried using the below code but the first value as shown will store -3(expected value 3) as the logic "subtraction-=i" will calculate 0-first value which will be negative.Please help me with the right process to follow.
Similarly,logic for division
Thanks.
What I have tried:
subtraction = 0
def sub(*x):
l2 =list(x)
l2.sort(reverse=True)
sortedValue = l2
for i in sortedValue:
global subtraction
subtraction-=i #first parameter should not change.but here the value becomes negative
print("Subtraction of numbers{0} = ".format(sortedValue),subtraction)
return subtraction
SubtractionResult = sub(20,2,5)
解决方案
Subtraction = 0 def sub(*x): l2 =list(x) l2.sort(reverse=True) sortedValue = l2 for i in sortedValue: global subtraction
You have two different spellings:Subtraction
andsubtraction
这篇关于如何减去传递给函数的动态值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文