Python Forum

Full Version: question about input, while loop, then print
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
#!/usr/bin/python3

'''
name #1 : april
name #2 : brett
name #3 : susan


>> create list
'''

count = 1
while (count <= 3):
    name = input ("Name #: ")
    count += 1

count = 1
while (count <= 3):
     print(" \n", count, name)
     count += 1

##########  end program
Output:
jamie@box3:~/python_tut$ ./list_sort.py Name #: brett Name #: april Name #: susan 1 susan 2 susan 3 susan
please, only the last item of the list is printing. Also, how could I add "count" variable to the input () function to add a digit to each input line? For example, Name #1: Name #2:, Name #3? Thanks. I spent quite a bit of time on this today.
Have you used lists yet? here is an example of appending items to a list and then loop through the items of the list.
names = []

for number in range(1, 4):
    names.append(f"name: {number}")


for name in names:
    print(name)
Output:
name: 1 name: 2 name: 3
See if you can incorporate a list into your code.
Better to get the names from a text file or list than enter them 1 by 1

path = '/home/pedro/Desktop/'
myfile = '100names.txt' # each name on a new line
names_data = open(path + myfile, 'r')
data = names_data.readlines # data is now a list of the names
names_data.close()
You can see all the names in data like Yoriz said:

for d in data:
    print(d)
If you want a to get 2 values on input:

num, name = input('Enter 1 number and 1 name, separated by 1 space ... ').split()
Note: num is a string, if you want a number: mynumber = int(num)

Have fun!
Another way

names = []
counter = 1

while True:
    name = input('Enter Name: ')
    if name == 'q':
        break

    names.append(f'{counter}. {name}')
    counter += 1

    for name in names:
        print(name)
Output:
Enter Name: john 1. john Enter Name: jane 1. john 2. jane Enter Name: sue 1. john 2. jane 3. sue Enter Name: bob 1. john 2. jane 3. sue 4. bob
(Sep-28-2021, 06:21 AM)Yoriz Wrote: [ -> ]Have you used lists yet? here is an example of appending items to a list and then loop through the items of the list.
names = []

for number in range(1, 4):
    names.append(f"name: {number}")


for name in names:
    print(name)
Output:
name: 1 name: 2 name: 3
See if you can incorporate a list into your code.

what do you think, thanks!

#!/usr/bin/python3
names = []


for number in range (1, 4):
    scan_input = input ("Enter your name: ")
    names.append(f"name: {scan_input}")

for name in names:
    print(name)
#!/usr/bin/python3
 
'''
name #1 : april
name #2 : brett
name #3 : susan
 
 
>> create list
'''
 name = [ ]
count = 1
while (count <= 3):
    name.append(input ("Name #: "))
    count += 1
 
count = 1
while (count <= 3):
     print(" \n", count, name)
     count += 1
 
##########  end program