"TypeError: can only concatenate str (not "列出为什么向我显示此错误") to str"?


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()

最新更新