在循环外打印最终声明

时间:2016-10-01 01:50:24

标签: python python-3.x

我正在尝试打印一份最终声明,上面写着“第50辆公共汽车到达HH:MM。”但似乎我的print语句仍在我的for循环中。我该如何解决它?此外,似乎我的输出仍然在括号中。我不知道如何摆脱它。对不起,我是编程新手。

import numpy as np
import math
import random

l=[]
for i in range (50):
    def nextTime(rateParameter):
        return -math.log(1.0 - random.random()) / rateParameter
    a = np.round(nextTime(1/15),0)
    l.append(a)
cum_l = np.cumsum(l)
print(cum_l)

print("The 50th bus arrives at ", cum_l//60,":", cum_l%60,2)

1 个答案:

答案 0 :(得分:0)

Print不在for循环中。您认为print位于for循环中的原因是因为您打印的cum_llist。但是你只需要最后一个元素:

import numpy as np
import math
import random

rateParameter = 1/15

def nextTime(rateParameter):
        return -math.log(1.0 - random.random()) / rateParameter

l=[]
for i in range (50):
    a = np.round(nextTime(rateParameter), 0)
    l.append(a)

cum_l = np.cumsum(l)
#print(cum_l)

output = ""
last = len(cum_l) - 1

output += str(cum_l[-1]//60) + ":" + str(cum_l[-1]%60)

print("The 50th bus arrives at: ", output)