简体   繁体   English

在循环外打印最终声明

[英]Print Final Statement Outside For Loop

I'm trying to print a final statement that says "The 50th bus arrives at HH:MM." 我正在尝试打印最终声明,说“第50辆公共汽车到达HH:MM。” But it appears my print statement is still in my for loop. 但是看来我的打印语句仍然在我的for循环中。 How do I get it out of it? 我如何才能摆脱困境? Also, it seems like my output is still in brackets. 另外,似乎我的输出仍然放在方括号中。 I'm not sure how to get rid of that. 我不确定如何摆脱这种情况。 Sorry, I'm very new to programming. 抱歉,我是编程新手。

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)

Print is not in for loop. Print不在for循环中。 The reason you think that your print is in for loop is because you print cum_l which is a list . 您认为print处于for循环中的原因是因为您打印了cum_l这是一个list But you need only last element: 但是您只需要最后一个元素:

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)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM