Python 为用户输入拆分 def 函数参数
Python split def function parameter for user input
我正在尝试将 def 函数参数拆分为两个用户输入,然后将两个值相加然后打印出来。
示例代码:
def ab(b1, b2):
if not (b1 and b2): # b1 or b2 is empty
return b1 + b2
head = ab(b1[:-1], b2[:-1])
if b1[-1] == '0': # 0+1 or 0+0
return head + b2[-1]
if b2[-1] == '0': # 1+0
return head + '1'
# V NOTE V <<< push overflow 1 to head
return ab(head, '1') + '0'
print ab('1','111')
我想将 "print ab('1','111')" 更改为用户输入。
我的代码:
def ab(b1, b2):
if not (b1 and b2): # b1 or b2 is empty
return b1 + b2
head = ab(b1[:-1], b2[:-1])
if b1[-1] == '0': # 0+1 or 0+0
return head + b2[-1]
if b2[-1] == '0': # 1+0
return head + '1'
# V NOTE V <<< push overflow 1 to head
return ab(head, '1') + '0'
b1 = int(raw_input("enter number"))
b2 = int(raw_input("enter number"))
total = (b1,b2)
print total
我的结果:1,111
期待result:1000
您没有在第二个代码段中调用您的函数。
total = ab(b1,b2)
我不知道你是如何让 return 在这里工作的。
首先(如丹尼尔)所述,您有函数调用 missing/improper.
total = ab(b1,b2)
其次,您正在进行类型转换(将输入类型从 string
更改为 integer
)- 在您的函数 ab
中,您正在对 b1
和 b2
,这将导致异常:
Traceback (most recent call last):
File "split_def.py", line 33, in <module>
total = ab_new(b1,b2)
File "split_def.py", line 21, in ab_new
head = ab_new(b1[:-1], b2[:-1])
TypeError: 'int' object has no attribute '__getitem__'
最终的工作代码必须是:
def ab(b1, b2):
if not (b1 and b2): # b1 or b2 is empty
return b1 + b2
head = ab(b1[:-1], b2[:-1])
if b1[-1] == '0': # 0+1 or 0+0
return head + b2[-1]
if b2[-1] == '0': # 1+0
return head + '1'
# V NOTE V <<< push overflow 1 to head
return ab(head, '1') + '0'
b1 = raw_input("enter number")
b2 = raw_input("enter number")
total = ab(b1,b2)
print "total", total
我正在尝试将 def 函数参数拆分为两个用户输入,然后将两个值相加然后打印出来。
示例代码:
def ab(b1, b2):
if not (b1 and b2): # b1 or b2 is empty
return b1 + b2
head = ab(b1[:-1], b2[:-1])
if b1[-1] == '0': # 0+1 or 0+0
return head + b2[-1]
if b2[-1] == '0': # 1+0
return head + '1'
# V NOTE V <<< push overflow 1 to head
return ab(head, '1') + '0'
print ab('1','111')
我想将 "print ab('1','111')" 更改为用户输入。
我的代码:
def ab(b1, b2):
if not (b1 and b2): # b1 or b2 is empty
return b1 + b2
head = ab(b1[:-1], b2[:-1])
if b1[-1] == '0': # 0+1 or 0+0
return head + b2[-1]
if b2[-1] == '0': # 1+0
return head + '1'
# V NOTE V <<< push overflow 1 to head
return ab(head, '1') + '0'
b1 = int(raw_input("enter number"))
b2 = int(raw_input("enter number"))
total = (b1,b2)
print total
我的结果:1,111
期待result:1000
您没有在第二个代码段中调用您的函数。
total = ab(b1,b2)
我不知道你是如何让 return 在这里工作的。 首先(如丹尼尔)所述,您有函数调用 missing/improper.
total = ab(b1,b2)
其次,您正在进行类型转换(将输入类型从 string
更改为 integer
)- 在您的函数 ab
中,您正在对 b1
和 b2
,这将导致异常:
Traceback (most recent call last):
File "split_def.py", line 33, in <module>
total = ab_new(b1,b2)
File "split_def.py", line 21, in ab_new
head = ab_new(b1[:-1], b2[:-1])
TypeError: 'int' object has no attribute '__getitem__'
最终的工作代码必须是:
def ab(b1, b2):
if not (b1 and b2): # b1 or b2 is empty
return b1 + b2
head = ab(b1[:-1], b2[:-1])
if b1[-1] == '0': # 0+1 or 0+0
return head + b2[-1]
if b2[-1] == '0': # 1+0
return head + '1'
# V NOTE V <<< push overflow 1 to head
return ab(head, '1') + '0'
b1 = raw_input("enter number")
b2 = raw_input("enter number")
total = ab(b1,b2)
print "total", total