Search code examples
pythonpython-3.xgoogle-colaboratory

can not take input second time


I am writing a python code to find valid email/web address. Now the code is half done. I have to take a integer from the user then take some string from the user. My program will tell if they are valid mail/web address. After running the program 1 time it is saying "'str' object is not callable". Here is my code. The code is simple. It only has 2 functions. One for binary search and another for check valid email address. Now I can not find the problem why I can run the program just for one time.

def binary_search(item,my_list):
    found=False
    first=0
    last=len(my_list)-1
    while first <=last and found==False:
        midpoint=(first+last)//2
        if my_list[midpoint]==item:
            found=True
        else:
            if my_list[midpoint]<item:
                first=midpoint+1
            else:
                last=midpoint-1
    return found

def isValidEmail(email_list):
  number = ["0","1","2","3","4","5","6","7","8","9"]
  number.sort()
  #isValid = True
  if(email_list.count("@") != 1 ):
    return False
  
  email_list = email.split("@")
  first = email_list[0]
  last = email_list[1]
  if(binary_search(first[:1],number)):
    return False

  if(last.count(".") == 0):
    return False

  last = last.split(".")
  if(last[0]=="" or last[-1] == ""):
    return False
  
  return True

n = int(input())
input_list = []
email = 0
web = 0
for i in range(n):
  input_list.append(input())

for input in input_list:
  is_Valid_Email =  isValidEmail(input)
  if(is_Valid_Email == True):
    email = email + 1
    web = web + 1


print(f"Email,{email}")
print(f"Web,{web}")
  

Solution

  • The reason is because you use input, which is the name of a built-in function, as a variable name at the line:

    for input in input_list:
    

    The second time you execute the cell, when Python sees input() it no longer refers to the built-in input but to the input from your input_list.

    I'm also getting another error:

    ---------------------------------------------------------------------------
    AttributeError                            Traceback (most recent call last)
    <ipython-input-21-4d260c538c36> in <module>()
          1 for input in input_list:
    ----> 2   is_Valid_Email =  isValidEmail(input)
          3   if(is_Valid_Email == True):
          4     email = email + 1
          5     web = web + 1
    
    <ipython-input-18-02905daac63f> in isValidEmail(email_list)
          6     return False
          7 
    ----> 8   email_list = email.split("@")
          9   first = email_list[0]
         10   last = email_list[1]
    
    AttributeError: 'int' object has no attribute 'split'
    

    And that error is because isValidEmail takes as argument email_list, but then on the line email_list = email.split("@") you split the email variable which you haven't defined in that function, so Python takes the email variable from the outer scope which you have defined as email = 0.