1
tala = int(input('Skrifaðu magn af pening-->'))
if tala >= 0:
    fth = int(tala / 5000)
    remains = tala % 5000
    tth = int(remains / 2000)
    remains = remains % 2000
    th = int(remains / 1000)
    remains = remains % 1000
    fhr = int(remains / 500)
    remains = remains % 500
    hdr = int(remains / 100)
    remains = remains % 100
    fty = int(remains / 50)
    remains = remains % 50
    ten = int(remains / 10)
    remains = remains % 10
    fiv = int(remains / 5)
    remains = remains % 5
    one = int(remains / 1)
    print(d[tala])

打印部分是 我不熟悉语法的问题的主要焦点,但计划是使用 dict 来避免使用大量 if 来告诉代码如果数字大于 5000 或大于2000 等。基本上我有这样的想法(无效的语法 ofc)

d = {}
##d[Value between 0 and 4] or d[value between 5 and 9] etc.
d[0-4] = ("That would be {0} Krónur.".format(one))
d[5-9] = ("That would be {1} Fimmkallar and {0} Krónur.".format(one,fiv))
d[10-49] = ("That would be {2} Tíkallar, {1} Fimmkallar and {0} Krónur.".format(one,fiv,ten))
d[50-99] = ("That would be {3} Fimmtíukallar, {2} Tíkallar, {1} Fimmkallar and {0} Krónur.".format(one,fiv,ten,fty))

如果您想知道这段代码在做什么,如果我说输入一个高于 5000 的值

它会将其拆分(有点像 ATM)到 5000 张钞票、2000 张钞票、1000 张钞票、500 张钞票、100 张硬币等的多少倍。

这是一个解决 % 符号使用的学校项目(它是为 C# 制作的,但我正在通过在 Python 中完成我的 C# 作业来学习 Python)

主要问题是:如果有一种方法可以使该字典样式起作用,我很想知道如何。 次要问题是:如果有更好的方法来做到这一点,我很想听听你的想法。如果我应该做它(或它的一部分),其他方式也不会伤害知道。

编辑:我找到了一个半功能解决方案,但它并不完全漂亮。然而,它比我的替代方案更具可读性,所以总比没有好,没有人有比这更好的主意吗?:

#Dictionary START
    if tala < 5:
        num = 0
    elif tala < 10:
        num = 1
    elif tala < 50:
        num = 2
    elif tala < 100:
        num = 3
    d = {}
    d[0] =("Það eru {0} Krónur.".format(one))
    d[1] =("Það eru {1} Fimmkallar og {0} Krónur.".format(one,fiv))
    ...
#Dictionary END
    print(d[num])
4

3 回答 3

0

首先,创建一个字典,将单位名称映射到先前提取的值:

d = {"Kronur": one, "Tikallar": ten, "Fimmtiukallar": fty, ...}

您可以使用collections.OrderedDict来保留将项目添加到字典中的顺序。但是,正如其他人指出的那样,使用元组列表可能会更好,因为您更关心顺序而不是快速随机访问:

d = [..., ("Tikallar", ten), ("Fimmkallar", fiv), ("Kronur", one)]

现在,您可以遍历字典中的项目(那些不是0)并将它们连接成一个长字符串。(使用元组列表时,请使用for (key, val) in d而不是for (key, val) in d.items()。)

def get_string(d):
    s = "That would be "
    not_zero = [(key, val) for (key, val) in d.items() if val > 0]
    for i, (key, val) in enumerate(not_zero):
        if i > 0:
            s += ", " if i < len(not_zero)-1 else " and "
        s += "%d %s" % (val, key)
    s += "."
    return s

示例输出:

>>> d = {"Kronur": 4, "Tikallar": 0, "Fimmtiukallar": 2}
>>> print get_string(d)
"That would be 2 Fimmtiukallar and 4 Kronur."
于 2013-03-21T14:50:07.997 回答
0

这应该可以按您的意愿工作,而不是使用 a ,dict因为未使用查找,因此 dict失去了the 的优势

def test_func(value):
    """
        >>> test_func(0)
        'That would be 0 Kronur.'

        >>> test_func(4)
        'That would be 4 Kronur.'

        >>> test_func(5)
        'That would be 1 Fimmkallar and 0 Kronur.'

        >>> test_func(15)
        'That would be 1 Tikallar, 1 Fimmkallar and 0 Kronur.'

        >>> test_func(47)
        'That would be 2 Fimmtiukallar, 0 Tikallar, 1 Fimmkallar and 2 Kronur.'
    """
    twenty = value / 20
    ten = (value - twenty * 20) / 10
    five = (value - twenty * 20 - ten * 10) / 5
    one = (value - twenty * 20 - ten * 10 - five * 5)
    ranges = [((0, 5), 'That would be {0} Kronur.'),
              ((5, 10), 'That would be {1} Fimmkallar and {0} Kronur.'),
              ((10, 20), 'That would be {2} Tikallar, {1} Fimmkallar and '
                         '{0} Kronur.'),
              ((20, 50), 'That would be {3} Fimmtiukallar, {2} Tikallar, '
                         '{1} Fimmkallar and {0} Kronur.')]
    text = next(v for r, v in ranges if r[0] <= value < r[1])
    return text.format(one, five, ten, twenty)

http://docs.python.org/2/library/doctest.html

于 2013-03-21T15:43:20.567 回答
0

如果您首先指定您拥有的所有面额的列表,然后,您可以使用 for 循环遍历每个面额,然后使用列表推导式输出子字符串数组。这是一个带有一些测试的示例。

#specify array containing denominations as a tuple, amount and name
denominations = [(5000, 'five thousand'), (2000, 'two thousand'), (1000, 'five thousand'), (500, 'five hundred'), (100, 'one hundred')]

def print_break_down(total):

    counts = []
    for denomination in denominations:
        counts.append(total/ denomination[0])
        total = total % denomination[0]

    main = ' and '.join(['%s %s' %(count, denominations[i][1]) for i, count in enumerate(counts) if count>0])
    print 'That would be %s' % main

print_break_down(15000)
print_break_down(15100)
print_break_down(5300)
print_break_down(4000)
print_break_down(1600)
print_break_down(600)
print_break_down(100)
于 2013-03-21T15:09:22.387 回答