Tutorial #7 Python: Registration program using lists and "for" cycle
What Will I Learn?
- How to use the "For" loop to add or modify data to a list
- Validate some user input data
- Create a registration program with name and age
Requirements
- Basic knowledge of programming
- Have Python 3.6.4 installed
- Have Sublime Text or Ninja-IDE installed (optional)
Difficulty
Either choose between the following options:
- Intermediate
Tutorial Contents
Source
Welcome to this seventh tutorial of Python where we will learn in a very didactic way to use the "for" cycle, making a program that allows the registration of people with two lists, one that keeps the name and another one the age, it is to your liking and preference use more data, but for practical effect I consider that two is more than enough, remember that for the realization of this tutorial you must have read and practiced the previous ones, since it will be understood that you understand the tools explained above, let's start.
First, we will make a simple menu, where the user can add a new list, see the list already created, modify it and finally leave the program, I remind you that I use "Windows" so certain functions can change depending on what operating system use, before making the menu we declare two variables as lists, which are the ones that we are going to modify and as in the previous tutorial we are going to enclose the user with a "While" cycle in the program until he decides to leave it starting with the following code:
import os
name = []
age = []
w = "Welcome to the people register"
option = 5
while option == 5: #Program menu
print (w.center(50, "="))
print ("What would you like to do?")
print ("1 To create a new record")
print ("2 To see the current record of people")
print ("3 To edit a record")
print ("4 Exit the program")
while option > 4: #Verification that the user chooses a valid option
option = int(input('Select an option from 1 to 4: '))
if option > 4 or option < 1:
print ("Enter a correct option (1-4)")
We already have a small structure of how our program will be which will contain 4 blocks of codes for each option, we will make the instructions to create the list, the first thing we must take into account is how many people are going to register, so we ask the user for this information and we assign it to a variable, in this way we can use this data in our "for" cycle, remember that it is useful to go through the times we indicate a list and code instructions, that is, if the user enters that he wants to register 3 people, 3 times the cycle will be repeated, we will do it with the following code:
if option == 1: #Create the list
reg = int(input("How many people will you register:"))
for n in range(reg):
print ("Name of the person number",n+1,":")
name.append(input())
print ("Age of the person number",n+1,":")
age.append(int(input()))
option = 5
os.system('cls')
If you realize where I throw the messages for the user I place "n + 1" this I do because the value of "n" always starts at 0, and the user would not like to see what the person number 0 will register, what I simply add 1 to show an appropriate result to the user.
We go with the second block, here we simply wish to postrarle the user a list of people already registered, but we must ensure that we already create one, so we put a condition that has as purpose to check if the lists are empty, if you enter it return to the menu main force to create a list, but if the list is created simply we throw all the elements of it, that we achieve with the following code:
if option == 2: #See the list of people created
if name == [] and age == []: #We check that the lists are not empty
print ("There is no record created")
a = input("Press enter to return to the main menu")
option = 5
os.system('cls')
else:
for n in range(reg):
print("Person number",n+1,": ", name[n], "Age:", age[n])
a = input("Press enter to return to the main menu")
option = 5
os.system('cls')
We continue with the most interesting block, in this we will modify the list created, but we will only do it in the person indicated by the user, ie if he made an error in the name or age he can do it without problems leaving the other data intact in the program, for that we must show the list again and indicate the position in numbers of each registered person, immediately afterwards we ask him what number of the list he wants to modify, we validate his answer with a simple condition, now that important data that we request we add it within the cycle "for", when the value of "m" which is the variable we are using in this case is equal to "b-1" (remember that in the lists are counted from zero as a result we must subtract 1 from the number that you place the user) execute the action of changing the data in said position, we can see this with the following code:
if option == 3: #Modify record
if name == [] and age == []: #We check that the lists are not empty
print ("There is no record created")
a = input("Press enter to return to the main menu")
option = 5
os.system('cls')
else:
for k in range (reg):
print("Person number",k+1,": ", name[k], "Age:", age[k])
error = 0
while error == 0: #We check that it does not exceed the number of records
b = int(input("Enter the number corresponding to the person you want to modify: "))
if b > reg or b < 1:
print ("Number not valid")
else:
error = 1
for m in range (reg):
if m == b - 1:
name[m] = input("Write the new name: ")
age[m] = int(input("Indicate the new age: "))
option = 5
os.system('cls')
The last block is similar to the previous tutorial which assures us that the user is really going to leave the program, the final code would be as follows:
import os
name = []
age = []
w = "Welcome to the people register"
option = 5
while option == 5: #Program menu
print (w.center(50, "="))
print ("What would you like to do?")
print ("1 To create a new record")
print ("2 To see the current record of people")
print ("3 To edit a record")
print ("4 Exit the program")
while option > 4: #Verification that the user chooses a valid option
option = int(input('Select an option from 1 to 4: '))
if option > 4 or option < 1:
print ("Enter a correct option (1-4)")
if option == 1: #Create the list
reg = int(input("How many people will you register:"))
for n in range(reg):
print ("Name of the person number",n+1,":")
name.append(input())
print ("Age of the person number",n+1,":")
age.append(int(input()))
option = 5
os.system('cls')
if option == 2: #See the list of people created
if name == [] and age == []: #We check that the lists are not empty
print ("There is no record created")
a = input("Press enter to return to the main menu")
option = 5
os.system('cls')
else:
for n in range(reg):
print("Person number",n+1,": ", name[n], "Age:", age[n])
a = input("Press enter to return to the main menu")
option = 5
os.system('cls')
if option == 3: #Modify record
if name == [] and age == []: #We check that the lists are not empty
print ("There is no record created")
a = input("Press enter to return to the main menu")
option = 5
os.system('cls')
else:
for k in range (reg):
print("Person number",k+1,": ", name[k], "Age:", age[k])
error = 0
while error == 0: #We check that it does not exceed the number of records
b = int(input("Enter the number corresponding to the person you want to modify: "))
if b > reg or b < 1:
print ("Number not valid")
else:
error = 1
for m in range (reg):
if m == b - 1:
name[m] = input("Write the new name: ")
age[m] = int(input("Indicate the new age: "))
option = 5
os.system('cls')
if option == 4:
R = input('Do you really want to leave the program?: y/n ')
if R == "y":
os.system('exit')
else:
os.system('cls')
option = 5
In this way we finish this seventh tutorial, remember that if you have any questions use the comments section, try to practice the "for" cycle at the beginning is a bit confusing but once you have the agility you can make infinities than things with it. See you at the next one!
Curriculum
- Tutorial #1 Python: Declaración de variables y Tipo de datos
- Tutorial #2 Python: Comentarios y Operadores Aritméticos
- Tutorial #3 Python: Tuplas y Listas
- Tutorial #4 Python: Diccionarios y Operadores Racionales
- Tutorial #5 Python: Operadores lógicos y Control de flujo.
- Tutorial #6 Python: Calculator with menu and data verification using the while loop.
Posted on Utopian.io - Rewarding Open Source Contributors
Thank you for the contribution. It has been approved.
One advice for your next tutorials: If you style your tutorials with sub-titles and more paragraphs they will gain a better readability.
You can contact us on Discord.
[utopian-moderator]
Thank you very much for the advice @flauwy, I will try to do it in the next tutorial
Hey @gabox I am @utopian-io. I have just upvoted you!
Achievements
Suggestions
Get Noticed!
Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x