r/learnprogramming • u/Pompomcry • 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 ( ̄∇ ̄)
17
u/elon_mosque_420 Jul 01 '22
Python: for item in mylist: print(item)
Javascript: for(let item of mylist){ console.log(item); }
C++: for(auto item : mylist){ cout << item << endl; }
Java: for(<type> item : mylist){ System.out.println(item); }
Since you didn't specify the language you needed, I included 4 of the common ones. IMO these are the most common languages taught.
In c++, I am unsure if the approach works for arrays (int[], char[], etc) but it should work for vectors and sets (and other data structures as well probably).