Python-for循环忽略if语句

时间:2018-09-21 19:03:50

标签: python-3.x

我正在尝试编写一个Python程序,该程序将以块的形式打印出99瓶啤酒的歌词,我已经开始工作了。但是,我需要在最后一行说“ 1瓶啤酒”,而不是“ 1瓶啤酒”。

我将代码包装在if语句中,但是if语句似乎被忽略了。我在做什么错了?

verse = '''
{some} bottles of beer on the wall
{some} bottles of beer
Take one down, pass it around
{less} bottles of beer on the wall
'''

verse2 = '''
{some} bottles of beer on the wall
{some} bottles of beer
Take one down, pass it around
1 bottle of beer on the wall
'''

for bottles in range(99 , 1, -1):
  if bottles >= 2:
    print(verse.format(some = bottles, less = bottles - 1))
  else:
    print(verse2.format(some = bottles))

2 个答案:

答案 0 :(得分:2)

尝试一下:

PHP
$History_Assoc = mysqli_fetch_array($HistoryQuery);


      echo '<ul class="repoFolder" data-value=" {lat: '.(float)$History_Assoc['latitude'].', lng: '.(float)$History_Assoc['longitude'] .'}  ">';
      echo '<br>';



JAVASCRIPT
var folders = document.getElementsByClassName("repoFolder");
for (let i = 0; i < folders.length; i++) {
  folders[i].onclick = function() {rootFolder.call(this)}; // note that I am passing in the context to rootFolder. 
}

function rootFolder() {

  var variable =  this.getAttribute('data-value');

  alert(variable);

  var markersHistory = new google.maps.Marker({
    position: variable,
    map: map,
    icon: iconBlue2,
  });
}

结果应为:

print(list(range(99 , 1, -1)))

数字1从未出现。

要解决此问题,只需将[99, 98, 97, ..., 5, 4, 3, 2] 更改为range(99, 1, -1)

答案 1 :(得分:1)

范围不包含端点,因此范围的最后一个元素不是1而是2。

您应该将if语句改为if bottles > 2:,输出将是正确的。