Learn to Code via Tutorials on Repl.it!

← Back to all posts
Learn to code Python! Part 3: For and While!
CoderBean (11)

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:
      accounts.remove(items)

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:
   body

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!

Cheers,
ChickenTendys
Part 1
Part 2

Comments
hotnewtop
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 (6031)

Hey,

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