- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有这个代码:
def time24hr(tstr):
if ('a' and ':') in tstr:
if tstr[:2] == '12':
tstr = tstr.replace('12', '00').replace(':','').replace('am','hr')
return tstr
elif len(tstr[:tstr.find(':')]) < 2:
# If length of string's 'tstr' slice (that ends before string ':') is less than 2
tstr = tstr.replace(tstr[:tstr.find(':')],'0' + tstr[:tstr.find(':')]).replace(':', '').replace('am','hr')
# Replace one-character string with combination of '0' and this one-character string. Then remove colon, by replacing it with lack of characters and replace 'am' string with 'hr'.
return tstr
else:
tstr = tstr.replace(':', '').replace('am', 'hr')
return tstr
elif ('p' and ':') in tstr:
tstr = tstr.replace(':', '').replace('pm', 'hr')
return tstr
else:
return "Time format unknown. Type correct time."
当我执行此代码 print time24hr('12:34am')
时,它会按原样返回 0034hr
字符串。它也适用于 print time24hr('2:59am')
,返回 0259hr
。但是当我在其中键入带有 12
的字符串时,它会自动省略这部分代码 if ('a' and ':') in tstr:
或此 elif ('p' 和 ':') 在 tstr:
并继续到这部分:
if tstr[:2] == '12':
tstr = tstr.replace('12', '00').replace(':','').replace('am','hr')
return tstr
因此无论我输入 12:15am
还是 12:15pm
,这段代码如果在字符串中找到 12
,就会开始执行此操作以上代码。 print time24hr('12:15pm')
返回 0015pm
但应该返回 0015hr
且仅适用于包含 am
的字符串它。否则,不要将 12
更改为 00
并返回 1244hr
表示 12:44pm
。
我的问题是,为什么那些逻辑检查 if ('a' and ':') in tstr:
和 elif ('p' and ':') in tstr:
不工作?此代码旨在作为此测验的解决方案 -> http://www.pyschools.com/quiz/view_question/s3-q8
============================================= ===================================
感谢您帮助我进行逻辑操作。
另外,我已经完成了上面提到的测验,下面是工作代码:
def time24hr(tstr):
if (len(tstr[:tstr.find(':')]) == 2) and (tstr[0] == '0'):
tstr = tstr.replace(tstr[0], '')
if ('a' in tstr) and (':' in tstr):
if tstr[:2] == '12':
tstr = tstr.replace('12', '00').replace(':', '').replace('am', 'hr')
return tstr
elif len(tstr[:tstr.find(':')]) < 2:
# If length of string's 'tstr' slice (that ends before string ':') is less than 2
tstr = tstr.replace(tstr[:tstr.find(':')], '0' + tstr[:tstr.find(':')]).replace(':', '').replace('am', 'hr')
# Replace one-character string with combination of '0' and this one-character string. Then remove colon, by replacing it with lack of characters and replace 'am' string with 'hr'.
return tstr
else:
tstr = tstr.replace(':', '').replace('am', 'hr')
return tstr
elif ('p' in tstr) and (':' in tstr):
if tstr[:2] == '12':
tstr = tstr.replace(':', '').replace('pm', 'hr')
return tstr
elif len(tstr[:tstr.find(':')]) < 2:
PmDict = {'0':'12','1':'13', '2':'14', '3':'15', '4':'16', '5':'17', '6':'18', '7':'19', '8':'20', '9':'21', '10':'22', '11':'23'}
tstr = tstr.replace(tstr[:tstr.find(':')], PmDict[tstr[:tstr.find(':')]]).replace(':', '').replace('pm', 'hr')
# Replace every "number" (which is string literally) with it's corresponding "number" in 24HR format, found in 'PmDict' dictionary. Then, as in above cases, remove colon ':' by replacing it with lack of character or space and then replace 'pm' with 'hr'
return tstr
else:
return "Time format unknown. Type correct time."
我没有根据 KISS 规则编写这段代码,如您所见 - 因为它有点复杂,但在 IMO 上运行良好。
可以在这里测试-> http://doc.pyschools.com/console
大家干杯,感谢您的帮助:)
最佳答案
if ('a' and ':') in tstr:
与
相同if ':' in tstr:
希望这能让您洞察问题所在。
可能替换为
if 'a' in tstr and ':' in tstr:
关于python - 嵌套的 If/Else 控制流在不应该执行时执行 - 可能是逻辑操作错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12007444/
这个问题在这里已经有了答案: Does an else if statement exist? (2 个答案) 关闭 4 年前。 相信大家在学习C的时候,都学过这样的语法: if (conditio
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
Java 11.6 在这个 BMI 计算器中,它将输入人的体重、高度并计算 BMI。 BMI 计算正确,但是在分类 BMI 方法中,程序会跳过 if 和 else if 检查,只打印出每次测试的“肥胖
我有以下代码,它根据 url 参数更改,然后隐藏表单上的选择选项。即 www.example.com?type=images 最终会有 20 多个不同的参数。我想知道比拥有大量 if else 更好的
在WikiPedia Binary Search 的文章中有一个名为Deferred detection of equality 的部分,其中介绍了二进制搜索的某种“优化”版本,如下所示: int b
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我在下面的代码中遇到错误。尝试了 if 和 elseif 的所有组合,但无法使其工作。 错误:未捕获的语法错误:意外的标记其他 if(disableAd == 0 && disableWeather
我是 Java 初学者,我正在做一些练习题来提高我的 Java 技能。 编写一个名为 season 的方法,该方法接受两个整数作为代表月份和日期的参数,并返回一个指示该月份和日期的季节的字符串。假设月
有没有可能在 JavaScript 中做这样的事情? if (name == 'foo') { exampleFunction('some_arg'); } else if (name ==
我有一段简单的代码来检查一些条件,我希望每个条件都产生一个唯一的输出,但如果没有满足条件,则产生另一个唯一的输出。 有什么方法可以创建仅在所有先前的 if 语句都失败时才触发的 else 吗?我知道下
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 8 年前。 Improv
我发现自己经常使用这样的模式: if (a > b) { foo(); } elseif (c > d) { bar(); } else { baz(); } 这里的要点是第二
Hive 使用 IF(条件,表达式,表达式),所以当我想做 if/else if/else if/else 时,我必须这样做: IF(a, 1, IF(b, 2, IF(c, 3, 4))) 有没有更
直到 if 和 else 具有共同的作用域: if (int x = foo()) { // .... } else if (int x = bar()) { // ... } else {
是否可以保证 if-else if-else if-else block 中的 ifs 会按照编写顺序进行测试。 我问这个是因为我经常尝试通过将最常见的情况放在首位来优化我的代码,我想知道编译器所做的
我有一个方法可以检查某些事情并根据这些检查返回一个 bool 值。它涉及一个单独的分支 If 部分,该部分按顺序检查大约 5 个条件。如果这些条件中的任何一个返回真,则该方法将返回真;。如果没有任何条
这个问题在这里已经有了答案: Yet Another Conditional Operator Nesting Question (2 个回答) Why is the conditional oper
有没有一种方法可以从一个事件运行多个 if else 语句?例如,尝试运行此命令,但它仅对类为“two”的元素执行第一个 if else,而不是对类为“三”的第二个元素执行。 $(".hamburge
我有一个类似这样的查询: SELECT SUM(`table`.points) AS total_points FROM table 这将返回类似这样的内容 | **points** |
我正在尝试对单选按钮组运行“存在检查”,以确定如果在组中选择了 2 个单选按钮中的 1 个(if 语句),如果选择了 2 个单选按钮中的另一个(否则 if语句)或者如果两者都没有选择(else 语句)
我是一名优秀的程序员,十分优秀!