简体   繁体   English

无法弄清楚为什么代码在Python 3中工作,而不是2.7

[英]Can't figure out why code working in Python 3 , but not 2.7

I wrote and tested the code below with Python 3 and it works fine: 我用Python 3编写并测试了下面的代码,它工作正常:

def format_duration(seconds):
    dict = {'year': 86400*365, 'day': 86400, 'hour': 3600, 'minute': 60, 'second': 1}
    secs = seconds
    count = []
    for k, v in dict.items():
        if secs // v != 0:
            count.append((secs // v, k))
            secs %= v

    list = [str(n) + ' ' + (unit if n == 1 else unit + 's') for n, unit in count]

    if len(list) > 1:
        result = ', '.join(list[:-1]) + ' and ' + list[-1]
    else:
        result = list[0]

    return result


print(format_duration(62))

In Python3 the above returns: 在Python3中,上面返回:

1 minute and 2 seconds

However, the same code in Python 2.7 returns: 但是,Python 2.7中的相同代码返回:

62 seconds

I can't for the life of me figure out why. 我不能为我的生活找出原因。 Any help would be greatly appreciated. 任何帮助将不胜感激。

The answers are different because the items in your dict are used in a different order in the two versions. 答案是不同的,因为你的dict中的项目在两个版本中以不同的顺序使用。

In Python 2, dicts are unordered, so you need to do more to get the items in the order you want. 在Python 2中,dicts是无序的,因此您需要做更多的事情来按照您想要的顺序获取项目。

BTW, don't use "dict" or "list" as variable names, it makes debugging harder. 顺便说一句,不要使用“dict”或“list”作为变量名,这会使调试变得更难。

Here's fixed code: 这是固定的代码:

def format_duration(seconds):
    units = [('year', 86400*365), ('day', 86400), ('hour', 3600), ('minute', 60), ('second', 1)]
    secs = seconds
    count = []
    for uname, usecs in units:
        if secs // usecs != 0:
            count.append((secs // usecs, uname))
            secs %= usecs

    words = [str(n) + ' ' + (unit if n == 1 else unit + 's') for n, unit in count]

    if len(words) > 1:
        result = ', '.join(words[:-1]) + ' and ' + words[-1]
    else:
        result = words[0]

    return result


print(format_duration(62))

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

相关问题 Python 中带有破折号的 Hangman - 无法弄清楚代码为什么不起作用 - Hangman with dashes in Python - can't figure out why the code is not working Python-我的部分代码不起作用,我不知道为什么 - Python- Parts of my code aren't working and I can't figure out why python 2.7-无法弄清楚如何使用模拟进行测试 - python 2.7 - Can't figure out how to test with mock 无法弄清楚为什么 PyDub 不起作用 - Can't figure out why PyDub is not working 我不知道为什么我的 web 抓取代码不起作用 - I can't figure out why my web scraping code isn't working 无法弄清楚为什么追加到列表不起作用 - Can't figure out why appending to list isn't working 我的 Python if-else 语句不起作用,我不知道为什么 - My Python if-else statements are not working and i can't figure out why 在我的代码中没有得到我需要的输出,我不知道为什么。 Python - Not getting the output I need in my code and I can't figure out why. Python Python 2.7:无法用BeautifulSoup4解析树 - Python 2.7 : Can't figure out how to parse a tree with BeautifulSoup4 无法弄清楚如何在pyspark 1.6和python2.7中使用LinearRegression - Can't figure out how to use LinearRegression at pyspark 1.6 & python2.7
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM