0

I am writing a python program that requests a name and age. Store that data in a List. After the user has entered data, request that the user enters 'y' or 'n' to continue. If they enter 'n', print the list of names and ages figures to the screen. If they enter 'y', repeat the first step.

This is what I have done.

keepAsking = True

# Keep looping for as long as keepAsking is equal to true
while(keepAsking == True):

    stdName = input(" Name: ")
    ageRecord = int(input("Age: "))


    name_array = list()
    age_array = list()      

    name_array.append(stdName)
    age_array.append(ageRecord)

# Create and set checkContinue
    checkContinue = True

    # Keep looping for as long as checkContinue is equal to true
    while (checkContinue == True):

        # Ask the user if they wish to continue, and store it in doContinue
        doContinue = input("Continue? ")

        # Did they enter yes?
        if (doContinue.lower() == "yes") :

            # If so, stop asking if they want to continue, but keep asking
            # everything else. Thus checkContinue is made false, but keepAsking
            # remains true
            checkContinue = False

        # Did they enter no?
        elif (doContinue.lower() == "no") :

            # If so, stop asking if they want to continue ...
            checkContinue = False


            print (name_array)
            print (age_array)

This is what its meant to show

Name: Faith Wisdom
Age: 28
Continue? [y/n] y
Name: Mitchell Train
Sales: 15
Continue? [y/n] n

Faith Wisdom           28
Mitchell Train         15

but with what I wrote, it's showing

Name: Faith Wisdom
Age: 28
Continue? [y/n] y
Name: Mitchell Train
Sales: 15
Continue? [y/n] n

Mitchell Train         15

The first entry is not saving in the list

Anthony Geoghegan
  • 11,533
  • 5
  • 49
  • 56
  • Your formatting is a bit off. Could you recopy and paste your code in the question, highlight it all and press the `{}` button (or `CTRL+K`) – Hoog Apr 25 '19 at 14:47
  • 1
    Possible duplicate of [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – quamrana Apr 25 '19 at 14:50
  • You don't need to use two while loops, and you can define your arrays outside the while loop, check my answer! – Devesh Kumar Singh Apr 25 '19 at 15:04

5 Answers5

3

You initialize your arrays in each step here:

while(keepAsking == True):
    name_array = list()
    age_array = list()

This will write over everything you saved at each step. Try taking the definitions out of your while loop:

name_array = list()
age_array = list()
while(keepAsking == True):
    # do other stuff as usual
Hoog
  • 2,280
  • 1
  • 14
  • 20
1

Some issues in your code.

  1. You are redefining name_array and end_array for every while loop, but you want to define the list outside the loop, and update it inside the while loop
  2. You have two while true loops, but you can just have one while loop and update keepAsking accordingly
keepAsking = True

name_array = list()
age_array = list()

while(keepAsking):

    #Get nane and input
    stdName = input("Name: ")
    ageRecord = int(input("Age: "))

    #Update name and age array
    name_array.append(stdName)
    age_array.append(ageRecord)

    #Get input from user
    doContinue = input("Continue? [y/n] ")

    #Based on user input, keep asking or stop
    if (doContinue.lower() == "y") :
        keepAsking = True

    elif (doContinue.lower() == "n") :
        keepAsking = False

So the output now will look like.

Name: Faith Wisdom
Age: 28
Continue? [y/n] y
Name: Mitchell Train
Age: 15
Continue? [y/n] n
['Faith Wisdom', 'Mitchell Train']
[28, 15]
Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40
1

As pointed by Hoog's answer name_array and age_array should be defined before and outside the while loop.

Disclaimer: I've modified the names of the variables according to the PEP8 style guide.

Option 1: keeping 2 lists

If you refactor check_option into a function you can call it recursively when the option selected by the user is not among the correct. Also, as the name and age lists have always the same length, you can zip them just before iterating over them and printing its contents together every iteration in the for loop.

name_array = list()
age_array = list()

def check_continue():
    response = input('Continue? [y/n] ')

    if response == 'n':
        return False
    elif response == 'y':
        return True
    else:
        print('Please select a correct answer [y/n]')
        return check_continue()

while(True):
    std_name = input('Name: ')
    age_record = input('age: ')

    name_array.append(std_name)
    age_array.append(age_record)

    if not check_continue():
        break
    else:
        continue

for name, age in zip(name_array, age_array):
    print(name, '\t', age)

Option 2: merging the lists

By using a tuple to pack both std_name and age_record variables, you suppress the need of ziping two lists by keeping all the n th iteration data in the n th index of a single list.

users = list()

def check_continue():
    response = input('Continue? [y/n] ')

    if response == 'n':
        return False
    elif response == 'y':
        return True
    else:
        print('Please select a correct answer [y/n]')
        return check_continue()

while(True):
    std_name = input('Name: ')
    age_record = input('age: ')

    user = (std_name, age_record)
    users.append(user)

    if not check_continue():
        break
    else:
        continue

for name, age in users:
    print(name, '\t', user)

Note: In Python there is a built-in module called array which provides the array object type. Because you might not want to include 'array' in the list's names. You might better want to think on a plural name describing the elements inside the list as the list are aimed to contain the same type of elements in contrast with tuples (although you are not forced to).

Community
  • 1
  • 1
jesteras
  • 141
  • 1
  • 4
0

Initialize your arrays outside the while loop because every time you will loop you will rewrite your array and you will save only the last answers.

name_array = list()
age_array = list()
# now you write your loop
0

I think you are over thinking this problem. Also, you need to append to your list properly, which is your main issue.

name_age = []
while 1:
    stdName = input(" Name: ")
    ageRecord = int(input("Age: "))
    name_age.append([stdName,ageRecord])
    doContinue = input("Continue? ")

    if doContinue[0].lower() == "n":
        for name, age in name_age:
            print(name, age)
        break
eatmeimadanish
  • 3,809
  • 1
  • 14
  • 20