为什么我的柜台只返回1?

时间:2017-11-29 03:16:46

标签: python

为什么我的柜台只返回1?我希望它能返回所有数字的值,我做错了什么?

File "/lib/python3.4/site-packages/flask_security/utils.py", line 341, in send_mail
    mail.send(msg)
  File "/lib/python3.4/site-packages/flask_mail.py", line 491, in send
    with self.connect() as connection:
  File "/lib/python3.4/site-packages/flask_mail.py", line 144, in __enter__
    self.host = self.configure_host()
  File "/lib/python3.4/site-packages/flask_mail.py", line 158, in configure_host
    host = smtplib.SMTP(self.mail.server, self.mail.port)
  File "/usr/lib/python3.4/smtplib.py", line 242, in __init__
    (code, msg) = self.connect(host, port)
  File "/usr/lib/python3.4/smtplib.py", line 321, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "/usr/lib/python3.4/smtplib.py", line 292, in _get_socket
    self.source_address)
  File "/usr/lib/python3.4/socket.py", line 512, in create_connection
    raise err
  File "/usr/lib/python3.4/socket.py", line 503, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused

2 个答案:

答案 0 :(得分:0)

return移到for循环外,由于return语句,循环在第一次迭代中结束,因为该计数器为1。

def custom_len(input):
    counter = 0
    for i in input:
        counter += 1
    return counter 

arr_input = input("enter arr :")
length = custom_len(arr_input)

print("length is ", length)

答案 1 :(得分:0)

当你return函数函数终止时。作为循环中的return,它以循环的第一个值返回并终止。您的代码应如下所示:

def custom_len(input):
  counter = 0
  for i in input:
    counter += 1
  return counter 
'''
here the program return a value and terminates.
i.e it won't execute following three lines. 
If you want to continue execution, you should move the above line of code to end of the program.
'''

arr_input = input("enter arr :")
length = custom_len(arr_input)

print("length is ", length)