Learn to Code via Tutorials on Repl.it!

← Back to all posts
Learn to code Python! Part 3: For and While!

Welcome back to Part 3 of my Python tutorial here on replit. Yes, this is the third one today but school is boring so here I am doing this. In the last tutorial we learnt about if, elif, and else. These are pretty much the most important thing in python ever.

So head on over to your code and look at elif func_select == 2:. We are going to be deleting accounts so we need to get information from the user about what account they want deleted. So first add this code inside that elif statement.

accDelete = input("account: ")

Next we are going to iterate through the list accounts. What is iterating says the padawan at the front, well iterating is looping. If I was to iterate through a string "Hello World", then I would be looping through each character of that string once. We can do this with a for loop, I will show you another type of loop in a second.

For Loop

A for loop works by iterating through each character in a string, or each item in a list. So what we want to do is iterate through each item in a list to find the specific account we will be deleting. So copy this code into the file.

for items in accounts:
    if items == accDelete:

So this is a for loop, it iterates through the thing it's being told to which in this case is accounts. You see items up there? Well items is a variable which is equal to the item or character that you are currently iterating through. So if items is equal to the account the person wants to delete then, remove items.

So, what if they capitalise the account wrong? Good question, we need to go up to if func_select == 1: and change something in here. So we can turn the account into all lowercase letters so that it is easier to find the correct account.

accCreateLow = accCreate.lower()

This turns all the characters into lowercase letters, we can change lower to upper to make them uppercase. Now do the same in elif func_select == 2:, then change the for loop accordingly, I will let you do that.

When testing this, make sure to add at 1 least value to the accounts and balance list because when adding an account the code itself won't update

And you are now done the account deletion! Congrats!

While Loop

Here is that other loop I was talking about. It is called a while loop, it does the same thing over and over until the condition is false.

while condition:

So it's pretty simple, we can make it say hello world 5 times.

i = 0
while i <= 6:
    print("Hello World")
    i = i + 1

This will print "Hello World" 5 times. Now <= means less than or equal to, you can do the same with >. See i = i + 1? This is what escapes the while loop, it adds 1 to i each time it loops through, so when i is > 5 it will stop. Now I challenge you to make a while loop that counts forever. I will show you how to do it in Part 4 if you can't do it. Make sure to comment below with your code for the counter.

That concludes Part 3 of my Python tutorial, I hope you enjoyed yourself, have fun and good coding!

Part 1
Part 2

ExpressMinicabs (0)

Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here!

alisahwilson (0)

AllAssignmentHelp.co.uk offers the best academic services in the UK and all across the world. With our online assignment help, you can easily top your college and university. We have the best writing team and their expertise in their areas of study. Our services are essay writing, case study, dissertation help, coursework help, homework help online, academic study help, etc. If you want to take do my assignment service, hire our assignment writer to help experts today. you can contact us anytime and buy assignment from experts at a very genuine cost.

lawessay (0)

One of my friends can give this post link and ask me to learn php from that website; I'm a newbie, so if anyone can advise me from A to Z on where I should start, that would be great. I have to handle my site essay help from now on because my friend is on vacation.

leschrapid (0)

A really amazing article. I love it so much!. - . 1v1 lol slope unblocked

arnoldmiler (0)

With over ten years of experience servicing clients nationally, we feel we are the Top Digital Marketing Company. We use digital platforms to share your story and bind your brand to clients, boosting your company and assisting you in reaching your full online potential.

JenDavis1 (0)

Hi! People, I’m new to coffee making, so maybe someone more experienced could give me a tip. What are the best beans for cappuccino? It’s my GF’s favorite drink, so I want to learn how to make it perfect. https://coffeelikeapro.com/coffee-beans/best-coffee-beans-for-cappuccino

adamsmith87 (0)

Add Ivacy google chrome best vpn extension on your google chrome browser and connect to any of their 3200+ servers and protect your online presence.

EmmaDiaz123 (0)

Want to know what students say about WritePaperFor.me? It is a good idea. I like these WritePaperFor.me reviews, they contained fresh info and facts about this service.

sylvia99w (0)

Dunno how to jumpstart your essay? Here you’ll get info on how to craft an A-grade assignment with ease! https://collegehomeworktips.com/how-to-write-a-critical-essay-top-guidelines-and-recommendations/ The work will be done in the blink of an eye!

TomBrown3608 (0)

The second time I got help in essay writing, and they met my short deadline again. I'm sure Speedypaper has professional writers, and I will definitely continue to use this site.

Jasminmark (0)

Even when clients tell writers online that they are ready to pay to write my assignment, they rarely manage to get the kind of excellence they are looking for. This is where they need to consider contacting a reliable service provider like Crowd Content for expert assistance.

jenniferaniston (0)

For loops can also be used for a set of other things, not just numbers. Is it true? Source: pay for essay UK of UK Writing Experts.

Adrian466 (0)

Recently I have discovered the Python is an easy language to learn and understand the basics of coding, How coding works for http://www.college-universities.com/articles-guide-how-to-write-winning-college-masters-thesis.php and there are different software available to code on. I am taking the python classes from the online source due to the covid. I am it will help me to discover more coding platform.

Johnjames14 (0)

I am trying to use this multiple input controlled source for POLY model and to get exact dimension. Using https://www.edugeeksclub.com/pay-for-research-paper/ is one of the best experience and I think it's a unique service for those students who want to improve their writing skills.

Bookie0 (6297)


You should concatenate all your tutorials series into one big tutorial instead of posting several ones! :D