Python Crash Course #5 - Control Flow

preview_player
Показать описание
In this Python crash course tutorial series, you'll learn all the basics of Python from the ground up.

🚀🥷🏼Get access to the Python Crash Course on Net Ninja Pro:

🔥🥷🏼 Access Premium Courses with a Net Ninja Pro subscription:

🔗🥷🏼 Check out Clear Code for more of his own tutorials:

Рекомендации по теме
Комментарии
Автор

Great course! Still there's one thing u can do to make it just amazing. U can share the files u r creating in the course, so we can keep them as revision notes.

farazhayder
Автор

I understood the exercise as follows,

1. Create a list from 1 to 5.
2. If value is 2 then print "the value is 2"
3. If not then print "the value is not 2"
4. If the value is 5 then run a while loop and print the string "the last item" 6 times.

So based on this this was my code,

a_list = [1, 2, 3, 4, 5]
for value in a_list:
if value == 2:
print("the value is 2")
else:
print("the value is not 2")

if value == 5:
counter = 0
while counter < 6:
print("last item")
counter += 1

HariPrasanth
Автор

Loving this course so far! Thank you both

jrumbawa
Автор

for the exercise it should've been

elif x == 5:
y = 0
while y < 6:
print('last item')
y += 1

since you asked if the value is 5 then print '...' 6 times,
what you did you just run it after the for loop has already ended. meaning you just run it after it already finished going over all the items in the list, and not currently at 5.

supershad
Автор

mylist = [1, 2, 3, 4, 5]
for x in mylist:
if value == 2:
print('the value is 2')
elif value == 5:
while value <= 10
print('last item')
value += 1
else: print('the value is not 2')

omarelkholy
Автор

list = [1, 2, 3, 4, 5]
for x in list:
print(x)
if x == 2:
print('the value is 2')
else:
print('the value is not 2')
if x == 5:
for x in range(6):
print('last item')

simondanielssonmusic
Автор

Enjoying your courses on Python and Godot! Thanks for the great lessons thus far!

I have a question about this exercise if anyone more knowledgeable could guide me a bit

Below is my solution to the exercise and it does not have a counter variable like you provided. however, it does produce the correct display. would this still be an acceptable way to write while loops? or should i avoid this practice?

a_list = (1, 2, 3, 4, 5)
for x in a_list:
if x==2:
print('value is 2')
else:
print('value is NOT 2')
while x<=10:
x += 1
print('last item')


Terminal output:
value is NOT 2
value is 2
value is NOT 2
value is NOT 2
value is NOT 2
last item
last item
last item
last item
last item
last item

SWHero_
Автор

list=[1, 2, 3, 4, 5]
for x in list:
if x==2:
print("The value is 2")
else:
print("The value is not 2. The value is: ", x)
if x==5:
counter=1
while counter<=6:
print(counter, "This is the last item")
counter+=1

hmashiqurrahmansohan
Автор

Hi 🥷, If you could make a Three.js course it would be amazing. There are few courses about the subject, and if you make a worthwhile course about it it will dominate the field. Keep going ❤

IB-AF
Автор

hey I noticed that my code runs 'last item' 6 times if I do: while exercise_counter <=5, and 7 times as you did, if I do: while exercise_counter <=6

vitaliykuz
Автор

I understood the assignment differently (I thought to print the last item on the list 6 times & not the string "last item").

list_items = [1, 2, 3, 4, 5]
for i in list_items:
if i ==2:
print("the value is 2")
elif i!=2:
print('the value is not 2')
elif i==5:
multiplier = 0
while multiplier <6:
print(list_items[-1])
multiplier +=1

klodimjerashaj
Автор

the solution to the exercise isnt correct. the question stipulated that the while loop should run if the value was 5. this is the corrected code with a counter showing how many times it was printed

exercise_list = [1, 2, 3, 4, 5]

for item in exercise_list:
if item == 2:
print(f'this item is 2')
elif item == 5:
counter = 0
while counter < 6:
print(f'last item - {counter}')
counter += 1
else:
print(f'this item is not value 2')

bartaikibii
Автор

indentation concept- for me it's executing if statement even indentation is not proper

AnkitMishra-thgb
Автор

I solved like this(based on my understanding)🙃.

ex_list = [1, 2, 3, 4, 5]
for i in ex_list:
if i==2:
print('the value is 2')
continue
else:
print('the value is not 2 it is ', end='')
print(i)
if i == 5:
while i <= 10:
i+=1
print('last item')

ashikulislamdev
Автор

Hi Ninja 🥷 lets start new course about ionic and capacitor

amiraghyan