I want to make it such that my code remembers the previous list that I inputted after putting my second list. Inputs work but it only shows the recent input list after wanting to show the previous information list.
def inputting():
print("Enter your name")
global name
name = input()
print("Enter your favorite subject")
global subject
subject = input()
print("Enter your height")
global height
height = input()
print("Enter your country")
global country
country = input()
inputting()
global information
information = [name, subject, height, country]
previous = information
print("Here is your basic social information about yourself: {} \n".format(information))
print("Do you wanna input new information? y/n")
choice = input()
if choice == "y":
inputting()
information[0:4] =[name, subject, height, country]
print("Here is your basic social information about yourself: {} \n".format(information))
print("Do you wanna see your previous information? y/n")
AC = input()
if AC == "y":
print(previous)
else:
print("Thank you.")
What I'm thinking right now is to create another list consisting of the same categories for the second input, but I feel it to be redundant. Is there a way such that you use one list only?
I want to make it such that my code remembers the previous list that I inputted after putting my second list. Inputs work but it only shows the recent input list after wanting to show the previous information list.
def inputting():
print("Enter your name")
global name
name = input()
print("Enter your favorite subject")
global subject
subject = input()
print("Enter your height")
global height
height = input()
print("Enter your country")
global country
country = input()
inputting()
global information
information = [name, subject, height, country]
previous = information
print("Here is your basic social information about yourself: {} \n".format(information))
print("Do you wanna input new information? y/n")
choice = input()
if choice == "y":
inputting()
information[0:4] =[name, subject, height, country]
print("Here is your basic social information about yourself: {} \n".format(information))
print("Do you wanna see your previous information? y/n")
AC = input()
if AC == "y":
print(previous)
else:
print("Thank you.")
What I'm thinking right now is to create another list consisting of the same categories for the second input, but I feel it to be redundant. Is there a way such that you use one list only?
Share Improve this question edited yesterday John Kugelman 362k69 gold badges550 silver badges595 bronze badges asked yesterday dale lapuz20dale lapuz20 71 bronze badge New contributor dale lapuz20 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 02 Answers
Reset to default 1You can simply use a copy:
previous = information.copy()
or reassign information
to a new list instead of mutating the existing list it points to at the second stage:
if choice == "y":
inputting()
information = [name, subject, height, country]
BUT the best is to NOT use globals, they just make your code hard to read / understand, and your life harder in the long run, instead you can return the list directly:
def inputting():
name = input("Enter your name\n")
subject = input("Enter your favorite subject\n")
height = input("Enter your height\n")
country = input("Enter your country\n")
information = [name, subject, height, country]
return information
# cutting out the outer prompting logic:
information = inputting()
previous = information
information = inputting()
print(previous)
To keep hold of earlier data, you need to make a separate copy. With lists
, the way to do this is using slicing: [:]
. That creates a completely independent copy. If you just do new_list = old_list
they'll both refer to the same list, so changing one will change the other. If you were working with dictionaries
, you'd use .copy()
to achieve the same thing.
The best way to avoid the problems associated with global variables is to pass data explicitly as arguments to functions and return results from functions. Here is an example of the code without them.
def get_user_info():
name = input("Enter your name: ")
subject = input("Enter your favorite subject: ")
height = input("Enter your height: ")
country = input("Enter your country: ")
return [name, subject, height, country]
def display_info(info_list, label="Current"):
print(f"Here is your {label.lower()} social information about yourself: {info_list}\n")
information = get_user_info()
previous_information = information[:] # Create a *copy* using slicing
display_info(information)
choice = input("Do you wanna input new information? y/n: ")
if choice.lower() == "y":
information = get_user_info()
display_info(information)
show_previous = input("Do you wanna see your previous information? y/n: ")
if show_previous.lower() == "y":
display_info(previous_information, "Previous")
previous_information = information[:]
print("Thank you.")