Python excercise 11


#1

I am new in the programming world and all, and right now I am stuck in almost every single little excercise in python. It is very frustrating, because I just don’t know how to do it, or how to solve problems. The thing is can sombody please explain some tips and tricks so I can understand programming a little bit?

You don’t have to explain the very basics from python until lesson 5, everything before 5 is what I do know I guess. you can start explaining from excercise 11 from Practice Makes Perfect.
also English is not my native language so I don’t understand every term.

so what I do know in this excersise is how to define the function, but then I don’t know anymore…
Define a function called count that has two arguments called sequence and item.

Return the number of times the item occurs in the list.

For example: count([1, 2, 1, 1], 1) should return 3 (because 1 appears 3 times in the list).

There is a list method in Python that you can use for this, but you should do it the long way for practice.
Your function should return an integer.
The item you input may be an integer, string, float, or even another list!
Be careful not to use list as a variable name in your code—it’s a reserved word in Python!



def count(sequence, item):


please explain!


#2

For this problem I would say to try and break the problem down into parts before you try and tackle it. Your end goal is to return the number of times the item argument appears in the sequence argument.

First thing you need to figure out is how to check if what is passed in your item argument is present in your sequence argument.


#3

Hopefully this helps a bit.

Firstly, if you have not done the HTML and CSS courses, I would suggest to start there. They are easier languages to start with. This will help you understand some basic concepts that you can use to build on in python. While you certainly can start in python, you may find value in doing those lessons first. It will likely take only a few hours and may help you understand stuff better.

As for other tips:

  1. Many languages (such as python) have their roots in algebra. Therefore, it can be valuable to brush up on solving algebra equations. The logic of algebra is the basis of the logic of most programming languages. Doing other logic puzzles can sometimes improve your programming skills too.
  2. Pay very close attention to following instructions and giving very clear instructions. This is another important programming skill. Instructions must be exact. I remember a few classroom exercises that focused on following instructions when I was in high school. They translated well in the world of programming. Some ways to practice this might be doing some logic puzzles in a “Penny” puzzle book or the like. Another way is to buy an inexpensive piece of furniture with “some assembly required” and follow the instructions exactly. The ability to follow logic and explain your own logic flow is a very important programming skill.
  3. Practice pattern recognition. Especially logical pattern recognition. Sudoku is ok at this, the occasional word search puzzle, and those “find the differences in these two pictures” puzzles found frequently in the comic section of the newspaper can also be helpful.

Those are a few tips. After that knowledge of the terminology can be helpful. Programming terminology can be difficult for native English speakers. As in all languages, knowing the syntax is quite important.

As for your specific question: These are my own personal definitions. I suggest a textbook if these definitions are not sufficient.

  1. A function is a piece of code that is called and does it’s thing then returns to the program. While some functions can do things on their own most return one or more variables back to where they were called in the program.
  2. An argument is a variable (or constant) passed to the function. The number of variables required is generally defined by the function being called.
  3. A method is a bit like a function. It is a way to tell an object to do something. Don’t get too hung up on the exact definition of a method (at this time). The difference between a method and a function is a bit nuanced at the beginning. When you progress further, it will become apparent. Besides I’ve heard programmers use method and function nearly interchangeably in casual discussion (because we know what each other is trying to say).

Part of the important thing to grasp in this particular lesson is that arguments passed to functions can be: integer(s), string(s), float(s), or even another list.

That last part can be hard to grasp.

This part of the exercise is trying to emphasize that. Ignore “There is a list method in Python that you can use for this, but you should do it the long way for practice.” That information is interesting, but, it is off point.

You are to define a function that takes two arguments. The number of items in a sequence of items. (You can’t use the word “list” as one of the variable names as it is a reserved word in Python).


Hopefully you find some value in my explanation. Sorry for the book. It seemed needed to help clarify the definitions.

Have yourself a great day. Keep coding :desktop_computer:

:slight_smile: X


#4

Thanks for the tips! but my biggest problem is actually that I don’t know how to apply the assignment in the excercise simply because I don’t know what to use. for example in practise makes perfect:

Define a function called count that has two arguments called sequence and item.

Return the number of times the item occurs in the list.

For example: count([1, 2, 1, 1], 1) should return 3 (because 1 appears 3 times in the list).

There is a list method in Python that you can use for this, but you should do it the long way for practice.
Your function should return an integer.
The item you input may be an integer, string, float, or even another list!
Be careful not to use list as a variable name in your code—it’s a reserved word in Python!

on line 2 you can read “Return the number of times the item occurs in the list.”, but only in the example are numbers so how do I know which number they mean? is it really obvious and am I just really not good enough for programming. I am a person where everything has to be very clear otherwise I don’t understand it that much…


#5

No problem.

Try not to think of arguments being passed as items.

They can be:

  1. integers
  2. strings
  3. floats
  4. lists

Remember that [1, 2, 3] is a list of numbers.

In order for the function to work on a list, one of the arguments must be a list :wink:

However, it will get more clearly defined later in the lesson. For now, just make a function that receives two arguments. :slight_smile:


#6

what is the problem in this lesson? because I don’t know which thing to use and what do you mean with ''how to check if what is passed in your item argument is present in your sequence argument."?


#7

like this

sequence and item are the arguments right?

def count(sequence, item):


#8

In your count function you are defining you have two arguments sequence and item. The goal of this exercise is to figure out if the item argument is also in the sequence argument.

count([1, 2, 3], 7)

So above your function “count” has two arguments [1, 2, 3,] which is your sequence argument and “7” which is your item argument. The goal would be to determine how many times item is in sequence, so in this example is 7 in the list? The answer is 0 so your function should return 0.


#9

ohhhh I get it now, but do I have to make an example like yours in order to work?


#10

I also just started with html like you suggested although it is still the beginning I do understand everything what they explained!


#11

Sorry for the delay, no you do not need to have an example like what I showed you in order to pass the exercise. I just wanted to demonstrate how it should be used once you finish coding the function.


#12

Good move. HTML and CSS will give you valuable skills on their own and will build a good foundation for more complex programming languages.

Code on!

:slight_smile: X


#13

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.