multiplication_table 函数打印传递给它的数字的结果

新手上路,请多包涵

multiplication_table 函数打印传递给它的数字乘以 1 到 5 的结果。一个额外的要求是结果不超过 25,这是通过 break 语句完成的。填空完成函数满足这些条件

def multiplication_table(number):
    # Initialize the starting point of the multiplication table
    multiplier = 1
    # Only want to loop through 5
    while multiplier <= 5:
        result = 1
        # What is the additional condition to exit out of the loop?
        if ___ :
            break
        print(str(number) + "x" + str(multiplier) + "=" + str(result))
        # Increment the variable for the loop
        ___ += 1

multiplication_table(3)
# Should print: 3x1=3 3x2=6 3x3=9 3x4=12 3x5=15

multiplication_table(5)
# Should print: 5x1=5 5x2=10 5x3=15 5x4=20 5x5=25

multiplication_table(8)
# Should print: 8x1=8 8x2=16 8x3=24

原文由 Frank 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 981
2 个回答

if result > 25:

multiplier += 1

原文由 Girly Corner 发布,翻译遵循 CC BY-SA 4.0 许可协议

代码:

 def multiplication_table(number):
# Initialize the starting point of the multiplication table
  multiplier = 1
# Only want to loop through 5
    while multiplier <= 5:
      result = number * multiplier
    # What is the additional condition to exit out of the loop?
      if result > 25 :
        break
      print(str(number) + "x" + str(multiplier) + "=" + str(result) , end =' ')
    # Increment the variable for the loop
      multiplier += 1
    print()

multiplication_table(3)
multiplication_table(5)
multiplication_table(8)

输出:

 3x1=3 3x2=6 3x3=9 3x4=12 3x5=15
5x1=5 5x2=10 5x3=15 5x4=20 5x5=25
8x1=8 8x2=16 8x3=24

原文由 rohanmech 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题