r/learnprogramming • • Jul 01 '22

Tutorial For loop NSFW

I’m having issues with the for loop. The assignment I’ve been given (due in 3 hours :’() is asking me to use a for loop to iterate over an existing list and display the list(there’s more but this is where I’m stuck right now)It’s a uni assignment and lord have mercy I need help ! Please anyone !!

Edit: I’m sorry for the lack of info it was 3 am and The dread of the deadline was starting to hit. Also I’m new

The language is python, the task is to create 2 functions and basic user interaction to book a theatre ticket. I’ve been given the theatre variable already in the program, all I have to do is create a “displayseats” and “bookASeat” function. The displayseats variable is required to iterate over the theatre list of lists and display whether or not a seat is booked using the letter ‘X’ or available using the letter ‘O’- specifically says not to use 0. Also says not to change the values in the ‘theatre’ list of lists in this function. This is task 1…… I think the fear of failing has caused my brain to go completely blank

Edit2: this subreddit doesn’t allow images :(( Basically the theatre is a bunch of O’s on the program but on my spec it’s A-F down and 1-12 across with A1-A3 being no seats and A10-A12 being no seats (same for B1-B3 and B10 and B12). Seats E&F are delux seats so I’m not sure if I should maybe like have a different function or code for that? I’m not sure what I’m doing at all. I’m reaching zen at this point ( ̄∇ ̄)

2 Upvotes

46 comments sorted by

View all comments

9

u/KenMan_ Jul 01 '22 edited Jul 01 '22

For (int x = 0; x < 10; x++){

}

You see everything in that parenthesis? Ya so from left to right, first we make x (int x = 0) Then we say as long as x is less than 10 (x < 10), we're gonna add 1 to x (x++)

Now, everything inside these -> {} is gonna occur, and then its gonna add 1 to x until x is GREATER than 9. Once its greater than 9, its done, it wont do anything inside of it. Because that would be illogical/retarded.

So now that you have a loop, you can do something 10 times. But whats really cool, is that you have the x which is changing each time.

So we can put a reference to a list, or an array, and pull out information at each number. So x=1 we can also do arrayName(x). The x is equal to 1 so its arrayName(1)

So you can make the x work for you in two ways at once. Its both counting for the loop, and referencing your list.

Pretty neat shit

Now your list may not be an array. But you can tell the program to read the next line in the lost instead. Boom

3

u/smidgie82 Jul 01 '22

The expressions inside the parentheses should be delimited with semicolons, not commas.

4

u/KenMan_ Jul 01 '22

You're correct, i've changed it

1

u/_ABear_ Jul 01 '22

does “int” mean integer or initialize?

6

u/KenMan_ Jul 01 '22

To initialize a variable is to give it a starting value.

So:

int x;

Is simply declaring an integer and naming it x.

But:

Int x = 0;

Is declaring an integer, naming it x, and initializing it to 0.

Great question

2

u/_ABear_ Jul 01 '22

Thanks fam

2

u/k4dota Jul 01 '22

integer