【问题标题】:"TypeError: can only concatenate str (not "list") to str" why this error is showen to me?“TypeError:只能将str(而不是“list”)连接到str”为什么会向我显示这个错误?
【发布时间】:2021-03-16 18:03:59
【问题描述】:
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

请不要点击“这个问题没有用”的标志,这对我没有帮助,我要求你宽容,如果你不明白,写在 cmets 中,我会尽力纠正

【问题讨论】:

  • 错误出现在哪一行?
  • 第17行出现此错误

标签: python string datetime utc python-re


【解决方案1】:

你有:

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

【讨论】:

  • 那我该怎么办??
  • 我什么都不懂
  • 您的比较if e=='us'or e=='usa' or ... 永远不会是真的,因为您将列表与字符串进行比较。如果你再次询问你应该怎么做,我建议你退一步看教程。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-02-06
  • 1970-01-01
  • 2022-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多