Python: every seven legs game

Scenario simulation: use the continue statement in the for loop to realize the calculation of the number of legs, that is, to calculate how many games whose mantissa is 7 or a multiple of 7 from 1 to 100 (excluding 100). The code is as follows:

total = 99                            # 记录拍腿次数的变量
for number in range(1, 100):          # 创建一个从1到100(不包括)的循环
    if number % 7 == 0:               # 判断是否为7的倍数
        continue                      # 继续下一次循环
    else:
        string = str(number)          # 将数值转换为字符串
        if string.endswith('7'):      # 判断是否以数字7结尾
            continue                  # 继续下一次循环
    total -= 1                        # 可拍腿次数-1
print("从1数到99共拍腿", total, '次。')  # 显示拍腿次数

Explanation: What the third line of code implements is: when the judged number is a multiple of 7, the continue statement on the fourth line will be executed, skipping the subsequent minus 1 operation, and directly enter the next cycle; similarly, the seventh line of code Used to judge whether it ends with the number 7, if yes, go directly to the next cycle

insert image description here

Guess you like

Origin blog.csdn.net/qq_45138120/article/details/132376049