Citizen Code: Season 1 Episode 3, difficulty understanding

Apprenti1970 Posted messages 6 Registration date   Status Membre Last intervention   -  
jee pee Posted messages 31912 Registration date   Status Modérateur Last intervention   -

Hello,

While doing some research on the Internet, I discovered the Citizen Code site, to learn Python through games. I think that's pretty cool.

I've reached episode 3 of Season 1. "The croquet game," with hoops to reposition, if I understood correctly. Hoop 8 needs to be moved to the grayed-out 8, etc... everything is offset by 1, I believe.

I started some code, at first, it's going well, but then it goes wrong.

I learned the for loop in range() :

I don't understand what I did wrong.

Also, the code can only contain 10 blocks.

I'm not asking for the solution, but to help me better understand, to show me a clue, just to shed some light on things.

Thank you,

Vincent

8 réponses

Diablo76 Posted messages 343 Registration date   Status Membre Last intervention   140
 

Hello,

If your goal is to learn Python, you can forget about Citizen, and install Python 3.13 (from the Store or the official site https://www.python.org/)

The IDLE is included in the Python installation; it will allow you to take your first steps and get familiar with and acquire the basic concepts (variables, lists, dictionaries, instructions, conditions, loops, procedures, etc.)

As for training courses, you can find a multitude of them online, particularly on OpenClassRoom.

You also have Swinnen, although it's not very recent, it will help you acquire the right fundamentals.

1
jee pee Posted messages 31912 Registration date   Status Modérateur Last intervention   9 947
 

Hello,

Are the clamp and the hoops in their starting position in the picture? Because I imagine the clamp should be in position 1, right?

When we look at your code, you aren't targeting far enough. You're one or two moves away. To write the shortest possible code (here 10 lines), you need to envision a process that looks far ahead. In this case, we're repeating the same action 8 times. That's precisely what programming is for – to repeat a basic operation 8 times, 1000 times, millions of times.

First thing, starting with the clamp in position 1, you need to move right 8 times to be above hoop 8. Then, you must define all the manipulations to pick up the hoop, move it to the right, place it down, and finally move left 2 times to end up above hoop n-1 (the first time being 7). Then you should have the instructions to create a sequence to execute 8 times for the 8 hoops.


0
Apprenti1970 Posted messages 6 Registration date   Status Membre Last intervention  
 

Hello,

The starting position is indeed above 8, as shown in the screenshot.

I was given the solution, I tried it and it works. However, I've tried to understand but I'm having difficulty. I'm not sure that this citizen code is the ideal solution for learning. Still, I want to learn and I can't find a worthwhile site. To start, a free one would have been nice. Understanding the basics, with small exercises.

Thank you for all your explanations. Here is the code that was given to me.

Have a great day,

Vincent

0
jee pee Posted messages 31912 Registration date   Status Modérateur Last intervention   9 947
 

This code corresponds to what I had indicated to you. If you look closely, it's also what you had started to do. Except that you were going to write instructions for each arch, one by one. You just needed to implement the repetition 8 times of what is the same sequence. Go back to your code and compare it with the solution, you just need the Loop(8).

These exercises are meant to train you in algorithmic thinking, with a very simple little robot. You must define the basic actions, with visual support, and the limitation of the number of lines forces you to think of a set of lines to repeat, to use the same thing several times. These are the principles of recursion, essential in programming.

When coding a program, we don't have the little robot; everything happens in our heads, where we try to make the program run. And if it gets complicated, we will draw a flowchart to represent the actions to be performed.

These first exercises are designed to lead you to think about how to organize the operations of a program. Afterwards, I think you will have courses to learn Python instructions, define variables and their values, inputs, outputs, and conditions.

Most training programs should start with Python instructions and gradually incorporate algorithmic concepts. Here, the course proposes to look at programming beyond the instructions themselves.


0
Apprenti1970 Posted messages 6 Registration date   Status Membre Last intervention  
 

Yes, you explained it to me. It is true that these little exercises are well done. For the code in question, I didn't understand why the second for loop in range is indented, whereas the first one is not.

In addition to citizen code, I'm trying to find a good YouTube channel for beginners in Python. And/or a good website.

Thank you for all the time you take to respond to me. That's kind.

If I may, I'd like to say that I really like your quote at the end of your response, "a stranger is a friend you haven't met yet."

Have a nice day,

Vincent

0
jee pee Posted messages 31912 Registration date   Status Modérateur Last intervention   9 947
 

Indentation, excellent observation, excellent question!

In most programming languages, formatting is primarily intended to facilitate and make the reading of code pleasant. In Python, it is a crucial component of the language. It is used to determine a block that consists of a set of instructions related to an instruction or a condition.

A block (*) starts with a line of instruction that ends with: All the lines of instruction in this block will have an indentation (at least one space, but conventionally 4 spaces) relative to the line creating the block. All instructions in this block will be aligned with the same indentation. The block ends when the indentation, relative to the original instruction, is no longer present.

For example

take() for loop in range(2): right place()

or 2 independent blocks

take() for loop in range(2): right() place() for loop in range(3): left()

But a block can contain another block. In this case, the lines of the second block have their own indentation relative to their original instruction. And a double indentation relative to the parent block. And if there are multiple levels of nested blocks, as many indentations as there are nested blocks.

Here the first for loop is at the main level of the program, at the root, it is the start of a block. The second is indented because it is within the block of the first, the instructions of this second block are indented relative to the second for loop.

for loop in range(8): take() right() place() for loop in range(2): left()

In other languages, to specify these blocks, curly brackets { } will be used, with different writing possibilities such as

for loop in range(2) { right() } for loop in range(2) {right()} for loop in range(2) { right() }

(*) on the Citizen Code site, the term block (within a maximum of 10 blocks) is used for what I call line, the block being a set of lines.


0
Apprenti1970 Posted messages 6 Registration date   Status Membre Last intervention  
 

I'm starting to grasp.

That said, how can I know when another block will be nested within the initial block and thus, that this new block will be indented relative to the first one...

In what case will it be (indented to the first block) and in what case will it not be? I'm not sure if my question is clear.

I don't know if you are familiar with the Citizen Code site. This is so you understand what I have learned. I haven't started the next exercise yet, for fear of getting confused.

For example, it doesn't seem to me that I saw on Citizen Code that a block of instructions could be indented to another.

What I didn't understand in your last message is the notion of "whitespace." At one point you talk about "4 whitespaces."

Thank you,

Have a nice day,

Vincent

0
jee pee Posted messages 31912 Registration date   Status Modérateur Last intervention   9 947
 

how to know if another block will be nested within the initial block and therefore, that this new block will be indented to the first...
it does not seem to me that I have seen on Citizen Code that a block of instructions could be indented to another.

The question does not arise in these terms. There are instructions that will have a set of lines of instructions associated with them, which gives a block of lines dependent on the initial instruction. Among these instructions are notably loops and conditions (the for, the while, the if). So every time one of these instructions is present, the lines following must be indented relative to the instruction that creates the block; if it was already indented itself, we will reach a second level of indentation. A for at a third level of indentation will add a new level of indentation.

Indenting code consists of introducing spaces also called whites at the beginning of the line. One white space is sufficient. But to be clear about the structure of the code, it is recommended to put 4.

Several months ago, following a question like yours asked on the forum, I went to Citizen Code (or maybe FutureEnginer, the two seem linked) to do some exercises and understand what this site was about. Because when we are given code like this saying it's Python and we have to find an error, it’s understandable to be skeptical.

for loop in range(8): prendre() droite() poser() for loop in range(2): gauche()

The only real Python instruction present is the for. The rest (prendre, droite, gauche, poser) are functions, developed to control the robot, which are in the "robot" library imported at the beginning of the program; they have names meaningful in context, but they could just as well be A, B, C, and D. You will never find these in a pure Python course. That is why I indicated that you are first learning the reasoning and logic of programming on this site, before looking in detail at the instructions of the language.


0
Apprenti1970 Posted messages 6 Registration date   Status Membre Last intervention  
 

Hello,

I don't understand everything, but it's starting to get a bit more fluid in my mind.

I think I've understood that Python is a language of clarity, which needs to be airy. Both for visual comfort and for reading.

To make sure I'm doing things right, in parallel, I would like to install Python on my PC, but which one? I use Windows 11, on Firefox. But I've also heard about downloading Python, and then there's IDLE, and it's in this latter that you have to type the code. Why not just install IDLE then...

Also, many people talk about Visual Studio Code. I'm a bit lost with all these names. I don't know what to do and what not to do anymore.

Ultimately, I have an ambition: I would like to create free genealogy software. Because yes, I love genealogy; I'm actually working on my own. I have absolutely no idea how to go about it! But it's an ambition. I imagine it's possible.

Thank you for the time you are giving me. I imagine that Python holds no secrets for you...

Have a nice day, in the rain, in Belgium.

Vincent

You know, I'm still very far from it, but

0
jee pee Posted messages 31912 Registration date   Status Modérateur Last intervention   9 947
 

I am an experienced computer scientist, but I have little experience with Python, which I discovered through forum questions. I have only used it for small scripts. I have the base version: https://www.python.org/downloads/ the standalone manager python-3.14.0-amd64.exe. With it, there is IDLE, which is a minimalist development environment, allowing code editing and execution launching. There are other more sophisticated environments that I don't know about; perhaps another participant can provide you with information.

With the intention of developing an application, it is important to complete a training course in full.

0
Apprenti1970 Posted messages 6 Registration date   Status Membre Last intervention  
 

Hello,

Openclassroom, noted. I do think that mastering the exact terms and the theory would be the foundation.

Thanks for the info, thanks for the book.

Vincent

0