"TypeError: can only concatenate str (not "list") to str" why this error is showen to me?
"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)
你好!我正在尝试创建一个代码,将我带回世界上不同的时区!虽然还没有完成而且我还处于起步阶段,但我不明白为什么会向我显示此错误
错误:
TypeError: can only concatenate str (not "list") to str
并且请不要点击“这个问题没有用”的标志,这对我没有帮助,我请你宽容,如果你不明白的地方写在评论中,我会尽力纠正
你有:
e=list(o)
它永远不会被 if 语句捕获,因为它是一个列表,而不是一个字符串。然后在下面你试图将一个字符串添加到列表中,因此出现类型错误。
mn=e[0].upper()+e[1:]
粗略看来,我认为您根本不想转换输入。
更改这两行:
o=input(f"{times}\n").lower()
e=list(o)
不转换为:
e=input(f"{times}\n").lower()
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)
你好!我正在尝试创建一个代码,将我带回世界上不同的时区!虽然还没有完成而且我还处于起步阶段,但我不明白为什么会向我显示此错误 错误:
TypeError: can only concatenate str (not "list") to str
并且请不要点击“这个问题没有用”的标志,这对我没有帮助,我请你宽容,如果你不明白的地方写在评论中,我会尽力纠正
你有:
e=list(o)
它永远不会被 if 语句捕获,因为它是一个列表,而不是一个字符串。然后在下面你试图将一个字符串添加到列表中,因此出现类型错误。
mn=e[0].upper()+e[1:]
粗略看来,我认为您根本不想转换输入。
更改这两行:
o=input(f"{times}\n").lower()
e=list(o)
不转换为:
e=input(f"{times}\n").lower()