简体   繁体   English

为什么我的 python 代码认为我试图连接<int>至<str>而实际上我不是?</str></int>

[英]Why my python code think i was trying to concatenate <int> to <str> while actually i wasn't?

I was trying to create a function timeConversion that take 12-hour time format as an input and return 24-hour time format.我试图创建一个 function timeConversion 以 12 小时时间格式作为输入并返回 24 小时时间格式。 The input is a string containing time in 12-hour format ie hh:mm:ssAM , hh:mm:ssPM .输入是一个包含 12 小时格式时间的字符串,即hh:mm:ssAMhh:mm:ssPM The hh , mm , ss represent hour, minute, and second respectively. hhmmss分别代表小时、分钟和秒。

The full code:完整代码:

def timeConversion(s):
     s = list(s)
     if s[-2:] == 'AM':  
        if s[:2] == ['1', '2']:
            s[0] = 0
            s[1] = 0
     else  :
        if s[:2] != ['1', '2']:
            s[0] = (int(s[0])*10 + int(s[1]) + 12) // 10
            s[1] = (int(s[0]+s[1]) + 12) % 10
        
      del s[-2:]
      s = ''.join(s)
      return s

x = timeConversion("07:05:45PM")
x

In line 10在第 10 行

s[1] = (int(s[0]+s[1]) + 12) % 10

I got the following error message:我收到以下错误消息:

TypeError: unsupported operand type(s) for +: 'int' and 'str'类型错误:+ 的不支持的操作数类型:“int”和“str”

I tried using我尝试使用

type(int(s[0]+s[1]))

and it return int type, so what did I miss?它返回 int 类型,那么我错过了什么?

In the line在行

s[1] = (int(s[0]+s[1]) + 12) % 10 s[1] = (int(s[0]+s[1]) + 12) % 10

You converted int(s[0]+s[1]).您转换了 int(s[0]+s[1])。 If s[0] = '1' and s[1] = '2', then it will give 12. But you need 3. So replace the line 10 with the following line..如果 s[0] = '1' 和 s[1] = '2',那么它会给出 12。但你需要 3。所以用下面的行替换第 10 行..

s[1] = (int(s[0])+int(s[1]) + 12) % 10

Maybe your list s contains strings, therefore也许您的 list s包含字符串,因此

s[0]+s[1]

causes the problem.导致问题。 Try尝试

int(s[0]) + int(s[1])

I see your problem.我看到了你的问题。

int(c[0]+c[1])

not equals to不等于

int(s[0]) + int(s[1])

In these two lines,在这两行中,

s[0] = (int(s[0])*10 + int(s[1]) + 12) // 10
s[1] = (int(s[0]+s[1]) + 12) % 10

the result is int which is causing the problem.结果是导致问题的int

def timeConversion(s):
    s = list(s)
    if s[-2:] == 'AM':  
        if s[:2] == ['1', '2']:
            s[0] = 0
            s[1] = 0
    else:
        if s[:2] != ['1', '2']:
            # change the evaluated result to str
            s[0] = str((int(s[0])*10 + int(s[1]) + 12) // 10)
            s[1] = str((int(s[0]+s[1]) + 12) % 10)
        
    del s[-2:]
    s = ''.join(s)
    return s
>>> x = timeConversion("07:05:45PM")
>>> x
'19:05:45'

After reading the solutions and comments i've noticed my mistakes.阅读解决方案和评论后,我注意到了我的错误。 Allow me to provide my own solution to the problems.请允许我为这些问题提供我自己的解决方案。 This solution required me to introduce an extra variable(which i try to avoid before, but failed miserably).这个解决方案需要我引入一个额外的变量(我以前尽量避免,但失败得很惨)。

def timeConversion(s):
    s = list(s)
    if s[-2:] == ['A','M']:  
        if s[:2] == ['1', '2']:
            s[0] = 0
            s[1] = 0
    else  :
        if s[:2] != ['1', '2']:
            #introducing variable k
            k = int(s[0]+s[1]) + 12
            s[0] = str(k // 10)
            s[1] = str(k % 10)
        
    del s[-2:]
    s = ''.join(s)
    return s

x = timeConversion("07:05:45PM")
x

the code above output: output上面的代码:

19:05:45

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError: can only concatenate str (not "int") to str (我认为这不应该发生) - TypeError: can only concatenate str (not "int") to str (I don't think that should happen) 为什么我在输入 str 或 int 后仍无法连接我的字典? - Why can't I get concatenate my dict even after i type str or int? 我在尝试在 python 中添加两个 integer 值时遇到此错误“只能将 str(不是“int”)连接到 str” - I am facing this error “can only concatenate str (not ”int“) to str” while trying to add two integer values in python 为什么我的代码认为我是在将列表与 int 进行比较? - Why does my code think I'm comparing a list to an int? (只能将str(而不是“int”)连接到str)这是我得到的错误为什么它说我正在尝试添加字符串和int? - (can only concatenate str (not “int”) to str) this is the error i am getting why is it saying that i am trying to add string and int? 你好。 我在执行代码时遇到了问题。 但是我得到一个错误'TypeError:只能将最后一个指针连接到str(不是“int”)到str' - Hi. I faced a problem while excuting my code. But I get an error 'TypeError: can only concatenate str (not "int") to str' for the last pointer 嗨,我正在努力使用非常基本的 python 代码。 我遇到一个错误说 TypeError: can only concatenate str (not "int") to str - Hi, I'm struggling with very basic python code. I face an error saying TypeError: can only concatenate str (not "int") to str 为什么python认为我没有定义我的变量? - Why does python think I haven't defined my variables? Python 的新手,试图修复 TypeError: can only concatenate str (not "int") to str in my savings calculator - New to Python, trying to fix TypeError: can only concatenate str (not "int") to str in my savings calculator 当我没有将 int 与其他字符串组合时,为什么在 python 中出现类型错误? TypeError:只能将str(不是“int”)连接到str - Why am I getting type error in python when I am not combining int with other string? TypeError: can only concatenate str (not “int”) to str
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM