Learn to code Python! Part 2: Data, if and variables!
Hello and welcome back to my Python tutorial, if you haven't already make sure to check out Part 1 of my Python tutorial series here on replit. In todays tutorial we are going to be experimenting with variables and user input, and data types.
Data types are very important because they are be used for data validation. Data validation is checking the data given and making sure it is correct. When you get incorrect data you can do lots of things to get around it, and one very easy way is to just tell people to do again correctly. But that is not what we are going to do, we are going to make a bank safe with username and password, and recreate the whole account creating process in our repl or .py file.
Create a file called account_setup or something, and we are going to first start with requesting a username and password because this is e-banking. We are going to make it e-banking because DATA VALIDATION and what not. First request a username and password, make sure your variables are called something meaningful so you and other people don't get confused.
username = input("username: ") password = input("password: ")
Great! That's done! But do we want to want the username and password to be a certain data type? Well first here are the ways to deal with invalid input:
Ignore the invalid input
Remove the invalid input
Modify the invalid input
Allow the invalid input to perform the same task as valid input
*Ask the user to re-enter the input if it is invalid
So password usually have to be a certain length so let's start with that, to get the length of a string we use the len() function. So copy this code into your file.
passLen = len(password)
Inside the parenthesis of len() we type the variable or string we want the length of. So now the data type of passLen is int, and has a value of the number of characters in password. I am going to make the minimum amount of characters of the password 8, and the maximum 12. So to check this we use an if statement, and if statement looks like this.
if condition: #: means then body
If the condition is true, then the code inside the body will be executed, but if it false nothing happens unless we add else. The code in else happens only when the condition of the if statement is false. So copy this code into your file (the minimum and maximum can be what you want).
if passLen < 8 or passLen > 12: print("password length must be greater then 8 and less then 12") else: print("account setup successful")
See how I have made the condition
passLen < 8 or passLen > 12: or literally means or. if the length of passLen is less than 8 or greater than 12 it will then run the body. if the condition is false the body of else will run.
We could also modify the else to an elif statement. elif means else if. (I would keep it as else but I will show you elif for educational purposes) elif works just like else but has its own condition.
elif condition: body
It is basically else and if combined, it must be underneath an if statement because when the if statement condition is false it goes to elif and checks the elif condition.
if passLen < 8 or passLen > 12: print("password length must be greater then 8 and less then 12") elif passLen > 8 and passLen < 12: print("account setup successful")
Now our login is working, we now need to be taken to an options page where we choose what to do. Lets make it!
print("1. Create account\n2. Delete account\n3. Check balance")
\n is an escape sequence, it starts a new line after that text, run the code to see.
Now that we have our options we are going to make a list called accounts, a list is a type of variable that store multiple variables at an index. A list has two square brackets  around it's data, it looks like this.
cars = ["BMW", "Ford", "Holden"]
Each piece of data is separated by a comma. To access items in a list we type the name of the list with square brackets next to them and the index position inside those brackets.
The index position is its position in the list, at index position 1 is
Ford. But ChickenTendys? Wouldn't index position 1 be BMW??? Ah great question young padawan, no! Python starts counting at 0, so BMW would be at index position 0! So create an empty list called accounts, data will be added later. Make another empty called balance.
Now we need to check for what the user wants to do, so we listed the options as 1 2 3 so we are going to check for those keys being pressed.
func_select = int(input()) if func_select == 1: #Account create code elif func_select == 2: #Account delete code elif func_select == 3: #Check balance code
Excuse me ChickenTendys? Yes youngling. Why do you have 2 equals signs? Good question, that is boolean, it checks to see if func_select = 2 is true. != is false. Now notice how we have input inside of int(). By putting input inside of int() we are turning the content into an int, meaning what the user inputs HAS to be numbers because Python cannot convert letters into and int because they are not numbers.
Now we need to do account creation. So we need to add accounts to the list so here is what we'll do, we will ask for the account name and initial balance, once we do that we will use the append function to add the account and balance to the end of the list. Put this code inside the first if statement.
accCreate = input("account: ") accBal = int(input("balance: ")) account.append(accCreate) balance.append(str(accBal))
Notice how accBal is inside str(). This is because currently the data type of accBal is an int, we don't need it to be a string but for the purpose of the tutorial I am making it a string. There is a function like this for every data type.
That's it for Part 2 of my Python tutorial series, we will continue this e-banking repl in the next few tutorials. I hope you learnt something and until next time, good coding!