num_rows=int(input('enter number here'))
first_row=[1]
second_row=[1,1]
print(first_row)
print(second_row)
for i in range(num_rows-2):
b=[1]
a=[1,1]
for i in range(len(a)-1):
b.append(a[i]+a[i+1])
b.append(1)
print(b)
a=b
for i in range(num_rows-2):
b=[1]
a=[1,1] # this sets a = [1,1] every time - you don't want to do this
for i in range(len(a)-1):
b.append(a[i]+a[i+1])
b.append(1)
print(b)
a=b
@JohnRennie num_rows=int(input('enter number here'))
first_row=[1]
second_row=[1,1]
print(first_row)
print(second_row)
a=[1,1]
for i in range(num_rows-2):
b=[1]
for i in range(len(a)-1):
b.append(a[i]+a[i+1])
b.append(1)
print(b)
a=b
I think you have a mixture of tabs and spaces as indents. That's probably messing up the indenting.
If I convert the tabs to the string "<TB>" your code looks like this:
num_rows=int(input('enter number here'))
first_row=[1]
second_row=[1,1]
print(first_row)
print(second_row)
a=[1,1]
for i in range(num_rows-2):
<TB>b=[1]
<TB>for i in range(len(a)-1):
<TB><TB>b.append(a[i]+a[i+1])
b.append(1)
<TB>print(b)
a=b
If you copied my code I generally put in spaces not tabs. That might be how you ended up with a mixture of spaces and tabs.
@JohnRennie num_rows=int(input('enter number here'))
first_row=[1]
second_row=[1,1]
print(first_row)
print(second_row)
a=[1,1]
for i in range(num_rows-2):
b=[1]
for j in range(len(a)-1):
b.append(a[j]+a[j+1])
b.append(1)
print(b)
a=b
num_rows = input("Please enter the number of rows: ")
num_rows = int(num_rows)
if num_rows > 0:
print("1")
if num_rows > 1:
print("1 1")
if num_rows > 2:
first_row = [1,1]
for i in range(num_rows-2):
next_row = [1]
for i in range(len(first_row)-1):
next_row.append(first_row[i]+first_row[i+1])
next_row.append(1)
print(*next_row)
first_row = next_row
When you use the * it prints the list without the brackets and commas i.e. just the list elements separated by spaces. This is just cosmetic but it looks good.
When publishers typeset books they have a tendency to convert the quotes used for programming, i.e. ' and ", to decorative quotes used for books.
The author is just warning you that this might have happened in his book. In fact the warning is not necessary since the publisher of the book hasn't done this.
But suppose you wanted to sum lots of lists. You don't want to retype those three lines every time as that's unnecessary hassle. Instead you define a function:
def sum_list(a_list):
total = 0
for i in range(len(my_list)):
total = total + my_list[i]
sum_list = total
So in the case of our example, to calculate the lowest factor, the function returns the lowest factor so we can use statements like y = lowest_factor(x)
# Define the function
def lowest_factor(x):
i = 0
for i in range (2,x+1):
if x % i == 0:
break
return i
# Test the function
for i in range(20):
print("The lowest factor of " + str(i) + " is: " + str(lowest_factor(i)))
Then the next unindented line marks the end of the function.
def maximum(a,b):
if a > b:
return a;
else:
return b;
print(maximum(1,2))
Like so.
Incidentally you don't need brackets with the return statement.
Though they do no harm.
# the function starts here
def maximum(a,b):
if a > b:
return a;
else:
return b;
# this unindented line marks where the function stops
print(maximum(1,2))
# the function starts here
def maximum(a,b):
if a > b:
return a
else:
return b
# this unindented line marks where the function stops
print(maximum(1,2))
Semicolons are used to mark the end of a line in C, and I'm so used to coding in C sometimes I put them in without thinking.
The first is when it calls maximum(1,2) and it prints 2, then the second id when it prints the value returned by the maximum function, and that prints None.
When you execute print(maximum(1,2)) this is equivalent to:
a = maximum(1,2)
print(a)
where a is a temporary variable that Python uses behind the scenes.
The name a isn't significant. I just chose a random name.
What you are doing is constructing a list from the string, so your function is returning a list not a string. So you are printing a list, and that's why it has spaces.