如何移动到下一个索引? [英] How do I move to the next index?

查看:63
本文介绍了如何移动到下一个索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个不同的货币清单,我想在每个循环后跳到下一个索引.我怎样才能做到这一点?我想在第一个循环之后使str除数等于[1],然后是[2],然后是[3],等等.

I have a list of different monetary notes and I want to jump to the next index after each loop. How can I do that? I want to make the str dividor equel to [1] after the first loop, then [2], then [3] etc.

money_list = [100,50,20,10,5,1,0.5]
dividor = money_list[0]

while change>0.5:
     print (change/100) + " X " + [0]
     change 
     dividor + [0]

推荐答案

为什么不循环遍历money_list数组?

why not loop over the money_list array?

无论如何,我猜您想输入一笔钱,并得到等额的零钱吗?

in any case, i'm guessing you want to be able to input an amount of money, and be given the equivalent in change?

这是我要怎么做:

#!/usr/bin/python
#coding=utf-8

import sys

#denominations in terms of the sub denomination
denominations = [5000, 2000, 1000, 500, 200, 100, 50, 20, 10, 5, 2, 1]
d_orig = denominations[:]


amounts = [ int(amount) for amount in sys.argv[1:] ]

for amount in amounts:

  denominations = [[d] for d in d_orig[:]]
  tmp = amount

  for denomination in denominations:
    i = tmp / denomination[0]
    tmp -= i * denomination[0]
    denomination.append(i)

  s = "£" + str(amount / 100.0) + " = "

  for denomination in denominations:
    if denomination[1] > 0:
      if denomination[0] >= 100:
    s += str(denomination[1]) + " x £" + str(denomination[0] / 100) + ", "
      else:
    s += str(denomination[1]) + " x " + str(denomination[0]) + "p, "

  print s.strip().strip(",")

,然后从终端;

$ ./change.py 1234
£12.34 = 1 x £10, 1 x £2, 1 x 20p, 1 x 10p, 2 x 2p

或者确实是数字的数量

$ ./change.py 1234 5678 91011
£12.34 = 1 x £10, 1 x £2, 1 x 20p, 1 x 10p, 2 x 2p
£56.78 = 1 x £50, 1 x £5, 1 x £1, 1 x 50p, 1 x 20p, 1 x 5p, 1 x 2p, 1 x 1p
£910.11 = 18 x £50, 1 x £10, 1 x 10p, 1 x 1p

这篇关于如何移动到下一个索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆