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

57

u/nogain-allpain Jul 01 '22

Two things would help here: 1) the language, and 2) any code you've tried so far.

3

u/Pompomcry Jul 01 '22

Language is python and so far I’m trying:

Def displayfunctionSeats ()

This is basically nothing. The function needs to return a value on whether or not that seat is booked or available

12

u/dota2nub Jul 01 '22

Google "how to return a value from a function python"

14

u/CutlerSheridan Jul 01 '22

My dude you gave us zero details

2

u/Pompomcry Jul 01 '22

I’ve updated with more details ! My bad

10

u/dmazzoni Jul 01 '22

OMG, it's been three hours. The suspense is killing me! Did /u/Pompomcry finish the assignment or not?

0

u/Pompomcry Jul 01 '22

barely started 🥲

61

u/149244179 Jul 01 '22

My mind reading powers are failing at the moment and I don't have time to hack into your computer to look at your code and identify the issue.

7

u/chrysthian95 Jul 01 '22

Truly true, we don't have crystal ball

16

u/[deleted] Jul 01 '22

What language are you using? They all work similar but the syntax may vary

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

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).

1

u/MasterDrake97 Jul 01 '22

C++: for(auto item : mylist){ cout << item << endl; }

if I'm not mistaken, this makes a copy

If you just want to print it, it would be better:

C++: for(const auto& item : mylist){ cout << item << endl; }

Looking for some corrections If I'm mistaken

2

u/elon_mosque_420 Jul 01 '22

My code might actually make a copy and your code should not. Thanks for adding this to my knowledge !!

4

u/MasterDrake97 Jul 01 '22

and thank you for not getting triggered :)

You never know

3

u/elon_mosque_420 Jul 01 '22

Lol I understand, but your comment made sense so didn't overreact.

1

u/Guard-Flashy Jul 01 '22

I wish you the best of luck! Wish I could help, I have an assignment due tomorrow working on the for loops in JS and another one due on Monday. It’s making me pull my hair lol

1

u/Pompomcry Jul 01 '22

It’s okay! GOOD LUCKKKK

1

u/VastRub7008 Jul 01 '22

For sure you need an array. What language are you using for the assignment??

1

u/Apprehensive_Plate60 Jul 01 '22

really cant find ans on google stackoverflow???

2

u/Pompomcry Jul 01 '22

I’m not exactly sure what I’m looking for if I’m being honest. I’m horribly new at this

2

u/[deleted] Jul 01 '22

For(int i = 0; i < {list}.Length; i++) { Console.WriteLine({list}[i]); }

Substitute {list} for your list variable.

2

u/Turbulent_Machine_33 Jul 01 '22

Also if you can post a snippet of the code you’ve written so far that’ll help too

2

u/Kinify Jul 01 '22

x = [“a”, ”b”, ”c”, ”d”]

X stores a list of strings a,b,c,d

You use the for loop to iterate inside of the loop. So for example. For i in x: Print(i)

This will give you all of the values in x

1

u/bravosix99 Jul 01 '22

Basically, a for loop(or just running through a loop in general), is basically going through something. In this case, you're just going through a list. But depending on the language, you'll do this differently(i''ll do it in java as an example)

for(int i = 0;i< array.length;i++){

System.out.println(arraylist[i]);

}

the variable(left) is the point where you start in the list, the length part (middle) is going through the list until you reach its size(so if its 5 things in the list, you should see 5 things), and the i++(right), goes to the next item in your array.

Simple as that.^

However(and as stated by some comments below), it would be more helpful if you told the Language and any code you tried so far..

Hope this helps!

1

u/TheRNGuy Jul 01 '22

use tabs in code

1

u/[deleted] Jul 01 '22

For i in (list): If i --------(booked): Return "X"

 else:
    Return "o"

1

u/[deleted] Jul 01 '22 edited Jul 01 '22

Change the (booked) with the condition

0

u/[deleted] Jul 01 '22

[deleted]

3

u/Marchingkoala Jul 01 '22

This made me laugh out loud

0

u/[deleted] Jul 01 '22

[deleted]

3

u/nogain-allpain Jul 01 '22

PMs are discouraged here, because others can benefit from the answers that the users here provide.

1

u/[deleted] Jul 01 '22

My bad! Didn’t know about that