有一个python类的赋值,我们必须编写一个程序,该程序要求输入,并将显示一个乘法表,其中包含输入之前的所有值。它要求我们使用嵌套的for循环。在
def mathTable(column, tuple):
for column in range(1, 13):
for tuple in range(1, 13):
print("%6d" % (column * tuple), end = '')
print("")
x = int(input("Enter the value of the multiplication table: "))
column = ""
tuple = ''
print(mathTable(column, tuple))
这就是它需要的样子:
如果您愿意使用第三方库,那么使用
numpy
这是很简单的:你在重用变量和参数名。也就是说,在创建for循环时,应该使用新的变量来循环,这样就不会覆盖参数、列和元组。我还将元组改为row以更清晰。您的循环变量可以命名为c和r,以保持简洁。在
另外,您硬编码了13作为您的表大小。您应该确保从1循环到列数,从1循环到行数。在
现在,如果你调用mathTable(3,4),你会得到一个乘法表
^{pr2}$好吧,在我的额头撞了几个小时之后,我意识到我是一个麻木的骷髅头。在
所以这就是最终完美工作的结果,我用第一个for语句定义了我请求的行数,然后第二个for语句是将这两个语句相乘的次数。 第一个for语句范围内的第二个参数直接与我的输入相关联,这允许我将表的输出更改为我想要的任何大小(请不要在其中输入9位数,永远不要在其中输入sentinel值,因此您必须关闭命令行才能对其执行任何操作)
最后,为了去掉表打印后出现的“NONE”文本,您需要记住,您已经创建了一个实际上不回答任何问题的函数,所以只要说{return(”“)}就适合我的目的,因为我已经实现了我要创建的输出。在
感谢那个帮助我的人我很抱歉当时我不理解你!在
相关问题 更多 >
编程相关推荐