所以我正在学习python作为初学者,并且一直在使用Python如何像计算机科学家一样思考python 3.我在关于迭代的章节中,从我自己的大脑做代码,而不是复制/粘贴,所以我记得它更容易。Python乘法表语法差异
在做乘法表部分的最后部分时,我得到了与本课程相同的输出结果,但看起来我的结构更清晰(参数更少)。我仍然在试图追踪追踪计划,所以我很难对这些差异进行包揽。我希望有人能让我知道,如果我的代码不如文本的版本更有效或更容易出错,并有助于结束这种头痛的问题;)。
def print_multiples(n, high): #This is the e-book version
for i in range(1, high+1):
print(n * i, end=' ')
print()
def print_mult_table(high):
for i in range(1, high+1):
print_multiples(i, i+1) #They changed high+1 to i+1 to halve output
这似乎是他们的结果将有太多+ 1的,因为我+ 1将成为print_multiples“高”,然后结束了在print_multiples'环+ 1再次增加。 (我还注意到,他们不停地结束=“‘而不是一个终点=’\ t”,这推翻了比对。
def print_multiples(n): #n is the number of columns that will be made
'''Prints a line of multiples of factor 'n'.'''
for x in range(1, n+1): #prints n 2n 3n ... until x = n+1
print(n * x, end='\t') #(since x starts counting at 0,
print() #n*n will be the final entry)
def print_mult_table(n): #n is the final factor
'''Makes a table from a factor 'n' via print_multiples().
'''
for i in range(1, n+1): #call function to print rows with i
print_multiples(i) #as the multiplier.
这是我的。在基本意见是为了我试图保持直线跟踪我的功能对我来说更有意义,但可能有一些差异,我不明白为什么本书决定为print_multiples()提供两个参数,因为1对我来说似乎足够了......我也改变了大部分变量是因为他们多次使用'i'和'high'来展示本地和全球。我重新使用了n,因为它们在两种情况下都是相同的最终数字。更有效的方法来做这种事情,但我仍然迭代。只是希望试着去感受一下什么是有效的,什么不可以,而这正在扰乱我。
我在这里没有得到相同的输出;你的每一行都是一个更短的元素。 –
尝试他们,我得到一个额外的专栏。课程页面显示我使用代码获得的输出,但使用代码时,表格中print_mult_table(7)的最终条目应为“49”时为“56”。我不确定我是否输入了他们的错误,或者他们在代码中犯了错误。 – Hesuchia