Search code examples
pythonstringdatetimeutcpython-re

"TypeError: can only concatenate str (not "list") to str" why this error is showen to me?


from datetime import datetime, timedelta
from pytz import timezone
import pytz
import re
while True:

    times=['Brazil','Hongkong','Australia','Mexico','US','Poland','Singapore','Portugal','Japan','Israel']
    use=[times[0],times[2],times[3],times[4]]
    print(r" enter the time zone that you want to watch ")
    o=input(f"{times}\n").lower()
    e=list(o)
    if  e=='us'or e=='usa' or e=='united state' or e=='untied states of america':
        uy=list(e)
        uy.remove(e[2:])
    else:
         mn=e[0].upper()+e[1:]
         if mn not in times:
             print("you need to enter time zone")
             continue
         else:
              if mn and uy in use:
              for i in  re.findall(f'^{mn}'|f'{uy}',pytz.all_timezones):
                  u=list(re.findall(f'^{mn}'|f'{uy}',pytz.all_timezones))
                  if u[i]=='/':
                      u.remove(u[0:i])
                      print(u)

Hello to you! I'm trying to create a code that takes me back to different time zones in the world! Although it is not finished yet and I am still in the beginning stages, I do not understand why this error is showen to me appears to me the error:

TypeError: can only concatenate str (not "list") to str

And please do not click on the sign "This question is not useful" It does not help me and I ask you to be tolerant and if you do not understand something write in the comments and I will try to correct


Solution

  • You have:

    e=list(o)
    

    which will never be caught by the if statement as it's a list, not a string. Then below you are trying to add a string to a list, hence the type error.

    mn=e[0].upper()+e[1:]
    

    From a cursory glance, I think you don't want your input converted at all.

    Change these two lines:

    o=input(f"{times}\n").lower()
    e=list(o)
    

    to without the conversion:

    e=input(f"{times}\n").lower()