开发者

Replace "while" with "for" in given python script

I have written following script in python which works fine:

from sys import argv

script, filename = argv

def brokerage(cost):
    vol = 100
    tamt = cost*vol
    br = (((cost*0.1)/100)*vol)*2
    rc = (0.0074*tamt)/100
    stt = (0.025*tamt)/100
    std = (0.004*tamt)/100
    stb = (11*br)/100
    tbr = br+rc+stt+std+stb
    return(tbr)

cost1 = 10
cost2 = 510

while cost1 < 501 and cost2 < 1001:
    value1 = brokerage(cost1)
    value2 = brokerage(cost2)
    # Can't write 'float' to file so, changed it into string. Don't know why.
    x1 = str(value1)
    x2 = str(value2)
    line = "  |%d\t\t%s\t|\t|%d\t\t%s\t|" % (cost1, x1, cost2, x2)
    # use 'a' in 'open' function to append it, will not overwrite existing value
    # as with 'w'.
    save = open(filename开发者_开发技巧, 'a')
    save.write(line)
    save.write("\n  |---------------------|\t|-----------------------|\n")
    save.close
    cost1+=10
    cost2+=10

Now instead of "while" I want to use "for" with minimal code structure change.


for cost1,cost2 in zip(range(10,501,10),range(510,1001,10)):
    # your code

or faster:

for cost1 in range(10,501,10):
    cost2 = cost1 + 500
    # Your code


for cost1, cost2 in zip(range(10, 501, 10), range(510, 1001, 10))):
    # remove these
    # cost1+=10
    # cost2+=10


for cost1 in range(10, 501, 10):
    # Your code here...
    cost2 = cost1 + 500

(not tested)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜