2017-09-03 128 views
0

python中Beginning classes在多行上打印字符串

我在家采取edx课程将我的技能集扩展到编程中。我遇到的其中一项任务让我难住。目标是能够插入一个整数并为其打印一个时间表。

该表将被分解为列和行。我可以将所需的值组合成一个字符串,用于给定变量输入的所有数字。我添加了在整数之间调用的选项卡。

现在这是一个字符串,我不能让它打破到不同大小的chinks和基于最初输入的不同值打印。

我试过文本打包,但无论如何我都把它放在不同的例子基础上。

请帮我找到一个解决方案,并解释其原因。我正试图学习这一点,没有一个可以解决问题的勺子代码,但仍然让我无法理解。

我在这门课的松懈中找到的提示都没有包含到目前为止课程中列出的术语或命令。很多没有列出的。

下面是我所拥有的,请忽略尝试不同解决方案时留下的额外费用。

mystery_int = 5 

#You may modify the lines of code above, but don't move them! 
#When you Submit your code, we'll change these lines to 
#assign different values to the variables. 

#This is a tough one! Stick with it, you can do it! 
# 
#Write a program that will print the times table for the 
#value given by mystery_int. The times table should print a 
#two-column table of the products of every combination of 
#two numbers from 1 through mystery_int. Separate consecutive 
#numbers with either spaces or tabs, whichever you prefer. 
# 
#For example, if mystery_int is 5, this could print: 
# 
#1 2 3 4 5 
#2 4 6 8 10 
#3 6 9 12 15 
#4 8 12 16 20 
#5 10 15 20 25 
# 
#To do this, you'll want to use two nested for loops; the 
#first one will print rows, and the second will print columns 
#within each row. 
# 
#Hint: How can you print the numbers across the row without 
#starting a new line each time? With what you know now, you 
#could build the string for the row, but only print it once 
#you've finished the row. There are other ways, but that's 
#how to do it using only what we've covered so far. 
# 
#Hint 2: To insert a tab into a string, use the character 
#sequence "\t". For example, "1\t2" will print as "1 2". 
# 
#Hint 3: Need to just start a new line without printing 
#anything else? Just call print() with no arguments in the 
#parentheses. 
# 
#Hint 4: If you're stuck, try first just printing out all 
#the products in one flat list, each on its own line. Once 
#that's working, then worry about how to organize it into 
#a table. 


#Add your code here! 
import textwrap 
a = mystery_int 
b = 1 
c = 0 
d = 1 
e = "" 
f = "" 
g = "" 
h = "\t" 
j = 1 
k = a*2 

for b in range(1,a + 1): 
    for c in range(1,a+1): 
     d = b * c 
     e +=str(d)+"," 
     f = str(e) 

g = str.replace(f,"," ,"\t") 

#textwrap.wrap(g,10) 
#print(g[0:int(k)]) 
print(g[:k]) 
+1

您可以使用“\ n”而不是“\ t”来获取新行而不是制表符。但是,从提示中可以看出,他们希望单独打印这些行,即为每行打印和打印一个单独的字符串。 – RuthC

+0

谢谢。是的,它是如何将字符串分解成不同大小的部分以匹配初始变量,并以这种方式打印它们,以便让我适合。 – rmcrow2

回答

1

就快,你只需要在内部循环的每次迭代后收集在一个列表中每一行的值,然后打印行值。

鉴于您基本上已经有了一个完整的解决方案,除了一些小错误,我将提供解决方案的完整演练和解释。

符号:会,我们使用mystery_int代替a,我们会改变b(外环增量)来i,并c(内环增量),以j,与惯例保持一致:

mystery_int = 5 

for i in range(1, mystery_int+1): 
    row = [str(i)] 
    for j in range(2, mystery_int+1): 
     row = row + [str(i*j)] 
    print('\t'.join(row)) 

输出:

1 2 3 4 5 
2 4 6 8 10 
3 6 9 12 15 
4 8 12 16 20 
5 10 15 20 25 

在排外环(i)迭代,并且所述内环(j)以上列。
在每一个新行中,我们都需要一个新列表,row,它只有一个元素。第一个元素是我们将要做的倍数(因此第一行的第一个元素是1,第二行的第一个元素是2,依此类推)。

请注意,我们将所有整数转换为字符串(i - >str(i)),因为我们最终希望将每行打印为空白分隔的序列。

  • 无论何时使用空格打印,即使要打印的内容都是由数字组成,也需要将该内容转换为字符串表示形式。 (你已经在你的代码中完成了这一点,但值得重申的一点是它是一个常见的绊脚石。)现在

,在内部循环(j),计算乘积的第(i*2i*3之后的每一列,...,i*mystery_int)。对于内循环中的每个通道,将新产品添加到row。当我们完成内部循环时,我们将有一个从整数i开始的行的乘法系列的完整列表。

此时,在移至下一行之前,打印出当前行。 join()方法是使用在.之前指定的分隔符连接列表中的元素的常用方法。

  • 例如,' '.join(row)将创建单空间分隔值的字符串:

    ' '.join(["1","2","3","4","5"]) 
    # '1 2 3 4 5' 
    
  • 我选择使用制表符分隔的字符串,如打印的输出具有更好的格式。 (由于一些行有双位数字及其他只有个位数的号码,一个单一的空间分隔,使列没有对齐。)

注:

  • 从教学的角度来看,用“基本”整数irow = [str(i)]初始化每个新的row似乎更直观。这为内部循环内部的其余行计算提供了可视化锚定。然而,它也是有效的(也许有点“清洁”)简单地初始化一个空列表,row = [],然后开始用j = 1内环:

    for i in range(1, mystery_int+1): 
        row = [] 
        for j in range(1, mystery_int+1): 
         row = row + [str(i*j)] 
        print('\t'.join(row)) 
    
  • 有了额外的模块,它可以完成同样的目标更简单,而且通常更快,代码。看起来你正在使用Python标准库,这就是为什么我一直在基本解决方案中的基础。但考虑到乘法表实际上是两个相同的矢量的外积,还可以使用Numpy模块,提供大量的快速数学运算:

    import numpy as np 
    print(np.array2string(np.outer(np.arange(1, mystery_int+1), 
               np.arange(1, mystery_int+1)), 
             separator='\t')) 
    

点是,当你开始使用Python更多的是为了完成一个给定的任务,而不是简单地学习编程基础知识,所以假设有一个适合你的需求的模块是合理的,这可以是一个真正的节省时间。有一个just about everything的Python模块!

+0

非常感谢。这就是我需要能够理解为什么会起作用的解释。我很感激你花时间把所有东西都弄出来。我正在学习python学习编程,将我的职业道路转变为编程和软件开发,所以我需要理解事情的原因不仅仅是解决每门课程的问题。我再一次感谢你。 – rmcrow2

+0

非常欢迎!祝你事业有所转变。如果此答案是对您的问题的最佳答案,请单击答案左侧的复选标记将其标记为已接受。 –