纳兰小寒
发表于 2018-3-24 21:46:01
def mysum(*numbers):
my_sum = 0
for x in numbers:
if x == abs(x):
my_sum += x
else:
my_sum -= abs(x)
return my_sum
graceasyi
发表于 2018-3-24 22:00:59
本帖最后由 graceasyi 于 2018-3-24 22:02 编辑
def mysum(*a):
s = 0
out = ''
for e in a:
s += e
if e>=0:
out += '+' + str(e)
else:
out += str(e)
out += '=' + str(s)
return out if out=='-' else out
print(mysum(8, -7, 12, 83, -99, 22, 0, 1))
fan1993423
发表于 2018-3-24 22:48:38
def mysum(*x):
a=
l=''
for b in a:
if b>=0:
l+='+'+str(b)
else:
l+=str(b)
print(l,'=',sum(a))
坑得飞起
发表于 2018-3-24 22:49:18
本帖最后由 坑得飞起 于 2018-3-24 22:57 编辑
def fun(*a):
s,ss=0,''
for i in a :
s+=i
if i>0 :
ss+='+'+str(i)
else :
ss+=str(i)
if ss=='+':
ss=ss
print(ss+'='+str(s))
o41003121
发表于 2018-3-24 23:38:27
def mysum(*numlist):
strsum = ''
for each in range(len(numlist)):
if numlist < 0 or each == 0:
strsum += str(numlist)
else:
strsum += '+' + str(numlist)
strsum += '=' + str(sum(numlist))
return strsum
print(mysum(7,12,83,-99,22,0,1))
nononoyes
发表于 2018-3-25 09:42:01
def mysum(*arg):
sum = 0
str1 =''
for i in arg:
sum = sum + i
if i <0:
str1 = str1+str(i)
else:
str1 = str1 +'+'+str(i)
str1 = str1 +'='+str(sum)
return str1
print(mysum(-7, 12,83, -99, 22, 0, 1))
idol
发表于 2018-3-25 09:49:36
def mysum(*args):
result = args
resstr = ''
resstr += str(args)
for each in args:
if each < 0:
resstr += str(each)
else:
resstr += '+'+str(each)
result += each
resstr += '='+str(result)
return resstr
steph0216
发表于 2018-3-25 10:28:59
def mysum(*args):
result = sum(args)
txt = str(args)
for i in args:
if i>=0:
txt+='+'
txt+=str(i)
return txt+'='+str(result)
jiamin_2010
发表于 2018-3-25 10:35:48
def mysum(*args):
i =len(args)
while i !=0:
print("%d"%(args),end ="")
i-=1
if i!=0:
print("+",end ="")
else:
print("=",end ="")
i = len(args)
j = 0
while i!=0:
j+=args
i-=1
print(j)
mysum(8,-2,-9,0,8,6,3)
8+-2+-9+0+8+6+3=2
咸鱼新手
发表于 2018-3-25 11:01:47
{:10_257:}
OnO_O0o
发表于 2018-3-25 12:19:16
乱拼乱凑总算是有了答案{:5_107:}
def mysum(*args):
temp = ''
for i in args:
if i < 0:
temp = temp + str(i) + '+'
continue
temp += str(i) + '+'
temp = temp + '='
print(temp,sum(args))
mysum(-1,2,0,-4,5,-6,-7)
lan358088
发表于 2018-3-25 12:35:11
本帖最后由 lan358088 于 2018-3-25 18:38 编辑
def mysum(*args):
new=[]
num=0
for i in args:
num +=i
for i in args:
if i<0:
aa=('-'+str(abs(i)))
else:
aa=('+'+str(i))
new.append(aa)
new.append('=')
new.append(str(num))
if int(new)>=0:
new=str(int(new))
print (''.join(new))
mysum(-7, 12,83, -99, 22, 0, 1)
Chase_Kas
发表于 2018-3-25 22:18:36
本帖最后由 Chase_Kas 于 2018-3-26 22:59 编辑
貌似加一个*号就可以传入多个参数了~~(刚开始做的时候真不知道怎么传递多个参数,42课还是43课作业就提到了哈哈,才回想起以前讲过,只是忘记了~~)
def mysum(*nums):
string = str(nums)
cnt = nums
for each in nums:
if each < 0:
string += str(each)
else:
string += ('+' + str(each))
cnt += each
return string + '=' + str(cnt)
print(mysum(-7, 12,83, -99, 22, 0, 1))
参考了4楼大哥的答案~~才想起讲字符串格式化的时候有一个格式化操作符辅助命令'+'可以显示正号~~厉害了~~还有eval()这个BIF也是第一见到~~涨姿势咯~~
def mysum(*nums):
string = str(nums)
for each in nums:
string += '%+d'%each
return string + '=' + str(eval(string))
print(mysum(-7, 12,83, -99, 22, 0, 1))
opserve123
发表于 2018-3-25 22:42:34
def mysum(*arg):
totle = 0
a = ''
for i in arg:
if i >= 0:
a += '+' + str(i)
else:
a += str(i)
totle += i
return a + '=' +str(totle)
nanayuri
发表于 2018-3-25 22:43:50
def mysum(*args):
sum = 0
str1 = ''
for each in args:
sum += each
if each >= 0:
str1 += '+' + str(each)
else:
str1 += str(each)
if str1 == '+':
str1 = str1
return sum,str1
p = mysum(-1,2,3,-2,5,0)
print(p + '=' + str(p))
YEWYEW
发表于 2018-3-25 23:26:51
def mysum(*args):
sum1=sum(args)
str1="%+d"*len(args)+"="+str(sum1)
return print(str1%args)
y=mysum(1,-1,0,5,6,7,-8)
grf1973
发表于 2018-3-26 13:48:10
def mysum(*num):
ss,s='',0
for n in num:
sign='+' if n>=0 else ''
ss+=sign+str(n)
s+=n
if num>0:
ss=ss
return ss+'='+str(s)
print(mysum(7, 12,83, -99, 22, 0, 1))
大头目
发表于 2018-3-26 14:36:27
#周末不开机。。。没鱼币,难受=。=
def mysum(*argv):
string = ''
result = 0
for i in argv:
if i < 0:
string += str(i)
else:
string += '+' + str(i)
result += i
result1 = '=' + str(result)
out = string + result1
print(out)
mysum(-7, 12,83, -99, 22, 0, 1)
lljlijlij
发表于 2018-3-26 16:18:22
def mysum( arg1, *vartuple ):
mysum=arg1
for var in vartuple:
mysum += var
print(mysum)
mysum(-7, 12,83, -99, 22, 0, 1)
qichq_7
发表于 2018-3-26 20:45:56
fan1993423 发表于 2018-3-24 22:48
def mysum(*x):
a=
l=''
第一个数字是正数时打印会是: +8+12+83-99+22+0+1=27,有点不合题意了哈。