Conversation started Oct 8, 2019 at 8:15.
Oct 8, 2019 08:15
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
You are setting a=[1,1] every time through the loop ...
No it should take updated value
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
ohkay
@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
still showing error
it's showing error or b.append(1) line
it's an indentation error but i don't see any
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.
Oct 8, 2019 08:31
i started rom scratch
@JohnRennie Break will break inner for loop right
maybe i should use it
or not
.....
No, I wouldn't use break. Your for loop already loops for only the required number of times, so there is no need to use break.
@JohnRennie Got it!
@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
final code is this
and it's working!
Cool :-)
Shall I post my code?
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
Oct 8, 2019 08:39
ah yes
i should add those if statement
Actually I should have used the name prev_row rather than first_row. Oh well.
@JohnRennie But your first and second rows are strings
the next are lists
shoudn't u covert the first and second into list
The first two rows don't need to be calculated so there's no need to make them lists.
The first two rows aren't used anywhere in the program.
Incidentally note I use print(*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.
Oct 8, 2019 08:44
oh i didn't see that in code
@JohnRennie Is that for list only
or for tuples
and sets
is that star
definitely star
now my code looks clean
I think it works for anything that is iterable. Try it yourself at the console.
ok
yeah.works for tuple and set
@JohnRennie Can you help me with functions
What does that line nean
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.
So you can safely ignore that sentence.
Oct 8, 2019 08:54
ohkay
I didn't understand what's going on here
A function is just a way of grouping together statements so they can easily be executed.
okay
Suppose you want to sum the elements in a list. You could use code like:
total = 0
for i in range(len(my_list)):
  total = total + my_list[i]
OK so far?
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
Oct 8, 2019 09:00
what is a_list
This defines a function called sum_list that takes a list as an argument, sums all the elements in it then returns the total.
a_list is a name for the argument to the function
So for example:
a = [1,2,3]
b = sum_list(a)
print(b)
Will print 6
a is variable
a-list is variable?
Those three lines are just an example of how I could use the sum_list function that I defined above.
I need to go meet someone now. I probably won't be back until this afternoon.
ohkay.we will continue in afternoon if u are there
 
3 hours later…
Oct 8, 2019 11:36
@JohnRennie Hi
@Aladdin hi
How much do you know about functions already?
nothing.I just started them today
it's like
writing a code and using it multiple times
Yes. I used the example of a function to sum elements of a list.
So a function takes one of more inputs and returns something. In my example the input is a list, and the function returns the sum of the elements.
what exactly s a_lis
a_list
When you write a function the general syntax is:
def function_name(inputs):
Where function_name is the name you give to your function, and inputs is the names you give to the inputs that are passed to the function.
Oct 8, 2019 11:48
ok
So for my function I used:
def sum_list(a_list):
So my function is called sum_list and the input to it is called a_list
what do you give to a_list
I could do:
a = [1,2,3]
b = sum_list(a)
print(b)
So in my function:
def sum_list(a_list):
the value of a_list will be a i.e. [1,2,3]
Oct 8, 2019 11:53
@JohnRennie If i write
a=(1,2,3)
will it do the sum?
You mean:
a = (1,2,3)
b = sum_list(a)
Yes, that will set b to 6
so a_list doesn't take list only
it can take tuple
it can take list
it can take set
If you look at the code it works for anything that is iterable.
Oct 8, 2019 11:57
yeah...i was thinking like it will take only list
because u wrote a_list
It wouldn't work if you passed it an integer because the code does;
for i in range(len(my_list)):
and that would give an error because you can't use len() with an integer.
yep
@JohnRennie It seems like the input is a
so what does _list mean?
@Aladdin I was thinking of lists when I wrote it, that's why I called the input a_list. You could call it anything.
ah okay
@Aladdin it's just a name. The underscore character _ is just a character in a name. It's not anything special in Python.
Oct 8, 2019 11:59
@JohnRennie If you give () instead of (a_list)
and then write
a=[1,2,3]
will it still do it
When you're defining the function the code in your function needs to have some way to refer to the input.
If I write:
def sum_list():
then how can I refer to the input in the code that makes up the function?
Would it be better if I wrote:
def sum_list(x):
  total = 0
  for i in range(len(x)):
    total = total + x[i]
  sum_list = total
e.g. I call the input x
I'm not sure what the problem is. This is simpler than you think ...
@JohnRennie def sum_list(x):
  total = 0
  for i in range(len(x)):
    total = total + x[i]
  sum_list = total



x=[1,2,3,4]

b=sum_list(x)

print(b)
This is showing none?
why
what is none
Let me try it ...
Oops,that's my mistake
It should be:
def sum_list(x):
  total = 0
  for i in range(len(x)):
    total = total + x[i]
  return total

a=[1,2,3,4]
b=sum_list(a)
print(b)
I was getting mixed up with a different language. In Python the return command tells the function what to return.
Try running that
Oct 8, 2019 12:12
works
Yes, sorry, my mistake.
can you show another example or this
Can you think of something I can use as an example?
and what does none mean
None is a built in value in Python. For a function None means the function doesn't return any value.
That was because I had not put in the return command, so my function wasn't returning anything.
Oct 8, 2019 12:14
umm finding the factor of number
@JohnRennie Wait
what is return
when do we use it
Well suppose we are writing a function that takes a number as input and returns the lowest factor of that number.
When we write the function and calculate that factor we use the return command to tell our function to return the factor we calculated.
Let's code this so I can show you.
The code will be something like:
for i in range (2,x+1):
  if x % i == 0 then:
    break
So if for example x was 9 then we try all integers starting with 2 and up to 9 to see which divide x exactly. OK so far?
Now let's define a function to do this. I'll call the function lowest_factor()
The code will be:
def lowest_factor(x):
  for i in range (2,x+1):
    if x % i == 0 then:
      break
  return i
Oct 8, 2019 12:20
if i write
So our function is called lowest_factor and it takes one argument called x.
print(i)
instead of return
That would make the function print the result, then the function wouldn't return anything.
You could write the function that way, but that limits what you can do with it.
ok
what do you mean by returning
and how is it diferent from print
Consider the function sqrt(x) i.e. just calculate the square root of the number x
Oct 8, 2019 12:24
ok
You pass the function a number, x, and you get back the square root. Yes?
When we say sqrt returns the square root we mean the function gives us back the square root when we call it.
i.e. if I do y = sqrt(x) then the variable y gets set to the value returned by the function sqrt
Oct 8, 2019 12:26
yep
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)
okay
i will try some combinations on function
Shall I show you the code for the example of calculating the lowest factor?
# 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)))
Try that ...
Oct 8, 2019 12:36
wait
i=0
for i in range(2,x+1)
if i is 0 ,it doesn't fall in range?
Well nothing is divisible by zero because division by zero isn't defined.
And everything is divisible by one, so there's no point testing for divisibility by one.
That's why I start at 2.
so why have u written i=0
i don't understand it
for 0 in range(2,x+1):
it's false ,right?
I have to define i before it's used in the loop, so I have to set it to something.
But it doesn't matter what I set it to because it is set in the for loop.
Oct 8, 2019 12:41
okay
got it
@JohnRennie For i in range(20):
print("The lowest factor of " + str(i) + " is: " + str(lowest_factor(i)))
why you used 20
I just chose a random upper limit.
I wanted to show te function working with a range of inputs
I need to go now. I'll be back later today or tomorrow morning.
 
20 hours later…
Oct 9, 2019 08:49
@Aladdin Java?
Oct 9, 2019 09:08
@JohnRennie last thing
Do u know html
@Aladdin hi. Yes?
@Aladdin yes
 
1 hour later…
Oct 9, 2019 10:24
#include <iostream>
using namespace std;



int main ()
{


int n, i, vote;


int one = 0, two = 0, three = 0, four = 0, five = 0, six = 0, seven = 0, eight = 0, faulty = 0;



cout << "Enter the number of voters : "; //Take input for number of voter
cin >> n;



for (i = 1; i <= n; i++)

{


cout << "Enter vote by voter " << i << ":";


cin >> vote;

if(vote > 8){
cout<<"Invalid Vote !!"<<endl;
}else
{

switch (vote)


{


case 1:

one++;


break;


case 2:

two++;


break;


case 3:

three++;
why this is giving wrong output in faulty case ?
Oct 9, 2019 10:35
sorry leave it , I modified it accordingly
 
24 hours later…
Oct 10, 2019 10:17
@JohnRennie Hi
Oct 10, 2019 10:39
@Aladdin hi
can we discuss functions
Yes,what do you want to know about?
write a program to know maxmum of two numbers
so i will start with:
def num(a,b):
@JohnRennie def num(a,b)

if a>b:
	return(a)

else:
	return(b)


print(num(1,2))

num(1,2)
It's good to get into the habit of using meaningful names for functions and variables. So can we call the function maximum, not num?
error.....
yes
Oct 10, 2019 10:45
A def statement starts a code block, so all the lines that you want to be part of the function have to be indented.
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))
why ;
Ah, sorry, there shouldn't be semicolons there
# 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.
def maximum(a,b):
  if a > b:
    print(a);
  else:
    print(b);

print(maximum(1,2))
what's wrong here
Oct 10, 2019 10:53
That gives me:
D:\rhs\Python>python test.py
2
None
Is that you get as well?
So print(maximum(1,2)) is printing None
Why would that be? Can you think why?
none is when function has no value
None is when the function doesn't return anything.
And print(maximum(1,2)) is printing the value returned by maximum(1,2)
so it gives none?
so one of these statements don't run
Oct 10, 2019 10:56
Your function doesn't have any return statements. Yes?
That's why it doesn't return anything.
so what is 2 there
All your function does is either print a or print b.
ok
so shouldn't the output be
just 2
Oct 10, 2019 10:59
But your program executes two print() statements.
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.
wait
why does return work but not print
why is second print none
but when we use return it skips?
If you do not have a return statement in your function Python automatically adds return None to your function without telling you.
That's why your function is returning None
Oct 10, 2019 11:06
What do you mean by:
why is second print none
but when we use return it skips?
def maximum(a,b):
  if a > b:
    print(a);
  else:
    print(b);

print(maximum(1,2))
instead of print,when we use return ,it skips the none at second statement
like what was happening when we used return instead of print for both statements
def maximum(a,b):
  if a > b:
    print(a);
  else:
    print(b);
This has a print statement but no return. So when you call this function it prints a number and returns None. Yes?
def maximum(a,b):
  if a > b:
    return a
  else:
    return b
This has a return statement but no print statement. So when you call this function it returns whichever number is bigger and doesn't print anything.
@JohnRennie def mul_list(x):
	mul=1
	for i in x:
		mul=mul*i

	print(mul)


x=[1,2,3,4,5]
print(mul_list(x))
why is output
120
None
Oct 10, 2019 11:14
Does mul_list return anything?
what do you mean by return
I mean what happens when you put a return statement in your function.
Suppose you wrote:
y = mul_list(x)
print(y)
What would that set the variable y to and what would be printed?
so print(y) doesn't have a value
so y is set to nothing
Correct. Your function has no return statement so Python automatically adds a return None and that means your mul_list returns None.
So the value of y will be set to None and print(y) will produce "None".
ah got it
@JohnRennie How d you reverse a string
Oct 10, 2019 11:25
The string class has a reverse method:
s = "abc"
s.reverse()
print(s)
@JohnRennie Any online matierial for string class operation
Actually I just tried my code and it didn't work. I've made a mistake somewhere ...
Aha. Lists have a reverse method but strings do not. Oh well.
You'll have to write your own reverse function to reverse a string.
Python has a few quirks and this is one of them. I can't think of any good reason why lists should have a reverse function while strings do not.
@JohnRennie Maybe because strings are immutable
@JohnRennie def reverse_string(x):
	c=[]
	for i in x:
		c.append(i)

	c.reverse()
	d=print(*c)
	return(d)


x=input('enter string here')

print(reverse_string(x))
I come up with this
but it has spaces......
can we modify this
Oct 10, 2019 11:35
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.
But your function is fine. We just need to not use a list. How about this:
output_string = ""
for i in x:
  output_string = i + output_string
ah yes
i forgot we can add strings like this
I need to go now. I'll be back later today and tomorrow morning as usual.
later i want to ask more wuestions
if you are free ofcourse
Oct 10, 2019 11:43
OK. I'll ping you when I'm back.
 
3 hours later…
Oct 10, 2019 14:42
@Aladdin hi
it's not working
oops
c = c + x[i]
def reverse_string(x):
	c=""
	for i in range(-len(x),-1,1):
		c=c+x[i]

	d=print(c)



	return(d)


x=input('enter string here')

print(reverse_string(x))
If you do c=c+i this just adds the iteger i to your output string.
returning none for this
how
Oct 10, 2019 14:44
d=print(c)
The print() function just prints whatever you pass it. It doesn't return a value.
So if you do this the variable d gets set to None
ohkay
Why have you got a call to print in your function anyway?
A function wouldn't normally print anything, unless it was a function like print() that is specifically designed to print something.
simply writing return c is okay?
@Aladdin yes
@JohnRennie is my step function right
i thnk it's incorrect
because it's not reversing
Oct 10, 2019 14:52
It's always work testing using the console:
>>> x = "abcd"
>>> for i in range(-len(x),-1,1):
...   print(x[i])
...
a
b
c
>>> for i in range(-1,-len(x),-1):
...   print(x[i])
...
d
c
b
That works but it's missing the last character.
Aha! Try:
>>> for i in range(-1,-(len(x)+1),-1):
...   print(x[i])
...
d
c
b
a
aha it's working
Cool :-)
@JohnRennie I found alternative method
def reverse_string(x):
	c=x[::-1]

	return(c)


x=input('enter string here')

print(reverse_string(x))
@Aladdin there are several different ways of reversing a string.
yeah i think slicing is much easier
Oct 10, 2019 15:06
@Aladdin oh yes :-)
@JohnRennie Do we need to install something for coding in html
like notepad is enough
Any text editor will do. Notepad is fine.
something about sublime text...someone pointed
Notepad++ is a popular free editor.
how to download
Oct 10, 2019 15:08
Or Microsoft have a free editor that does all sorts of clever stuff.
Oct 10, 2019 15:31
@JohnRennie Hi
@Aladdin hi
def a_list(x):
	for i in x:
		if x.count(i)>0:
			for j in range(x.count(i)+1)
				x.remove(i)


	return(x)



x=[1,2,3,4,5,6,6,7,7,7,7,8,8,9]
so i need to remove multiple elements from list
and only return the list with unique elements
but my program is showing error
We did this before ...
Sep 17 at 16:10, by John Rennie
That would change my code to:
Ah, that wasn't quite the same, but it used the same sorts of ideas i.e. look for repeated letters.
Actually, can't you just convert the list to a set? Doesn't tht automatically remove duplicates?
>>> x=[1,2,3,4,5,6,6,7,7,7,7,8,8,9]
>>> y = set(x)
>>> y
{1, 2, 3, 4, 5, 6, 7, 8, 9}
Although that's probably cheating as the point of the question is probably tomake you learn about manipulating lists.
but the problem is
we need to have unique elements
[1,1,1,1]
@Aladdin ah, OK, so converting to a set won't work.
Oct 10, 2019 15:41
i have to return the same list also..
So you have to modify the list passed to the function? Not create a new list containing only the unique elements?
okay we can create a new list
so it becomes easy
thought we have to modify the same list
Notes that lists have a count(element) method that returns a count of how many times the element occurs in the list.
@JohnRennie def a_list(x):
	a=[]
	for i in x:
		if x.count(i)==0:
			a.append(i)

	return(a)






x=[1,2,3,4,5,6,6,7,7,7,7,8,8,9]

print(a_list(x))
 
Conversation ended Oct 10, 2019 at 15:49.