如何使用while循环在python中创建乘法表? [英] How do I use while loops to create a multiplication table in python?

查看:29
本文介绍了如何使用while循环在python中创建乘法表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码,它输出一个乘法表,但这不是我想要的!

This is my code, it outputs a multiplication table but it's not what I wanted!

 num = int(input("Multiplication using value? : "))

while num <= 10:
    i = 1
    while i <= num:
        product = num*i
        print(num, " * ", i, " = ", product, "\n")
        i = i + 1
    print("\n")
    num = num + 1

我基本上是根据用户输入的 1-9 创建乘法表.

I am basically creating a multiplication table from the user's input from 1-9.

例如.如果用户输入3"

Ex. If the user inputs "3"

我应该得到这个输出:

1*1=1
1*2=2
1*3=3

2*1=2
2*2=4
2*3=6

3*1=3
3*2=6
3*3=9

这是我第一次学习Python,我可以在网上找到任何帮助,请帮助

This is my first time learning Python, I could find any help online, Pls Help

推荐答案

你手上出现无限循环的原因是因为你在比较 inum,同时每次运行时都会增加 num .如果你确保 i 总是 <=10,你会得到你想要的输出:

The reason why you have an infinite loop on your hands is because you are comparing i to num, while also increasing num on every run. If you make sure i is always <= 10, you get your desired output:

while num <= 10:
    i = 1
    while i <= 10:
        product = num*i
        print(num, " * ", i, " = ", product, "\n")
        i = i + 1
    num = num + 1
    print("\n")

这篇关于如何使用while循环在python中创建乘法表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆