Python Forum
question about input, while loop, then print
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
question about input, while loop, then print
#1
Exclamation 
#!/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.
Reply
#2
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.
Reply
#3
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!
Reply
#4
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
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#5
(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)
Reply
#6
#!/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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! ayodele_martins1 7 1,046 Oct-01-2023, 07:36 PM
Last Post: ayodele_martins1
  Code won't break While loop or go back to the input? MrKnd94 2 944 Oct-26-2022, 10:10 AM
Last Post: Larz60+
  Question: print issue python202209 3 953 Sep-18-2022, 11:51 AM
Last Post: jefsummers
  WHILE Loop - constant variables NOT working with user input boundaries C0D3R 4 1,470 Apr-05-2022, 06:18 AM
Last Post: C0D3R
  input function question barryjo 12 2,701 Jan-18-2022, 12:11 AM
Last Post: barryjo
Big Grin General programming question (input string)[ jamie_01 2 1,587 Jan-08-2022, 12:59 AM
Last Post: BashBedlam
  A question about 'Event loop is closed' fc5igm 2 2,195 Oct-05-2021, 02:00 AM
Last Post: fc5igm
  why print('\n') produced 2 new lines instead of 1 - Located inside a FOR loop JulyFire 2 2,512 Jan-10-2021, 01:50 AM
Last Post: JulyFire
  I want to check if the input is str or is int & if it's str repeat the loop HLD202 4 2,772 Nov-23-2020, 11:01 PM
Last Post: perfringo
  Loop back through loop based on user input, keeping previous changes loop made? hbkpancakes 2 2,927 Nov-21-2020, 02:35 AM
Last Post: hbkpancakes

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020