迭代期间对列表项感到困惑

时间:2019-04-11 20:05:04

标签: python for-loop

我正在检查是否可以在for循环中删除列表项,并在下一次迭代之前仍然使用它,而这种情况发生了。

<!DOCTYPE html>
<html>
<body>

<h2>Javascript CART breakdown calculator</h2>

<button onclick="pause_function()">pause</button>
<button onclick="resume_speed_function()">resume</button>


<label id="seconds">0</label>
<p id="random_number_display"></p>
<p id="breakdown_item"></p>


<script>

var my_random_number, low, high;
low = 1;
high = 5;
my_random_number = Math.floor(Math.random() * high) + low;
document.getElementById("random_number_display").innerHTML = my_random_number;

var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
var oscillator = audioCtx.createOscillator();

var random_breakdown_event, low_event, high_event;
low_event = 1
high_event = 9
random_breakdown_event = Math.floor(Math.random() * high_event) + low_event;
var text

if (random_breakdown_event == 1) text = "blown engine";
if (random_breakdown_event == 2) text = "broken transmission";
if (random_breakdown_event == 3) text = "broken suspension";
if (random_breakdown_event == 4) text = "broken halfshaft";
if (random_breakdown_event == 5) text = "broken cv joint";
if (random_breakdown_event == 6) text = "broken wings";
if (random_breakdown_event == 7) text = "electrical misfire";
if (random_breakdown_event == 8) text = "broken fuel pump";
if (random_breakdown_event == 9) text = "change battery";

var secondsLabel = document.getElementById("seconds");
var totalSeconds = 0;
var interval_between_ticks = 1000;
//Assign the interval to a variable to keep track of it
var interval = setInterval(setTime, interval_between_ticks);

function setTime() {
++totalSeconds;
secondsLabel.innerHTML = totalSeconds;
}

function  pause_function() {
interval_between_ticks = 999999999;
//Clear the old interval so we don't have memory leaks
clearInterval(interval);
//Set the interval with the new time between clicks
interval = setInterval(setTime, interval_between_ticks);
}

function  resume_speed_function() {
interval_between_ticks = 1000;
//Clear the old interval so we don't have memory leaks
clearInterval(interval);
//Set the interval with the new time between clicks
interval = setInterval(setTime, interval_between_ticks);
}


if(my_random_number==totalSeconds){
document.getElementById("breakdown_item").innerHTML = text}

//if(my_random_number>=4){
//function myFunction(frequency, duration, callback) {
//    duration = 10000 / 1000;     // the 10000 used to be 'duration' 
//    oscillator.type = 'square';
//    oscillator.frequency.value = 500; // value in hertz
//   oscillator.connect(audioCtx.destination);
//    oscillator.onended = callback;
//    oscillator.start(0);
//    oscillator.stop(audioCtx.currentTime + duration);
//}


</script>


</body>
</html>

结果:

items = ['1','2','3','4']
for i in items:
    items.remove(i)
    print(i)

我希望打印语句失败,或者所有数字都可以打印。我对for循环迭代的理解不足以理解结果。

0 个答案:

没有答案