Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multiple loops
#1
Hello, I'm writing a short program in Python 3.8 (Pycharm) with the following target: welcomes users in a party as they check in.
The steps are the following:
1- Ask the user how many guests are in the group.
2- Using a loop which runs based upon how many guests were input above, ask users for their name.
3- Print out for each user “Hello {name}, you are person {number} to check in today”
Example input:
2
Tom
George
Output:
Hello Tom, you are person 1 to check in today
Hello George, you are person 2 to check in today

I wrote the following:

# Step1: acquisition of the number of guests in the group
print("Please type how many guests are in your group:")
inputnum1=int(input())
print(type(inputnum1))
print("   ")
# Step2: loop for collecting the names of the guests
start=0
end=inputnum1
step=1
for idx in range(start,end,step):
  print("Please write your name here:")
  inputnames=input()
  print(inputnames)
print("   ")
# Step3: loop for the welcoming message of each guest: Hello {name}, you are person {number} to check in today
names=inputnames
id_guest=0
start=0
end=inputnum1
step=1
for idx in range(start,end,step):
 id_guest+=1
 idguest=str(id_guest)
 print(f"Hello "+names[idx]+", you are person "+idguest+" to check in today")[/color]
The problem with the above is that while the number (idguest) is incrementing, the names don't.
Can anybody help with this issue?
Thanks,
Paul
buran write Oct-11-2021, 08:51 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
I presume you want to keep the names in a list or similar structure. I don't see that happening in your code.

The step 2 loop runs the name input multiple times, but each time assigns it to inputnames, so each time through the loop it's just a string overwriting the previous input. Later you do names=inputnames, which seems to have no purpose. Names is also a string, so you can't index into it.

Probably you want to append each name to a list in the second loop.
Reply
#3
Thanks, so how can I get for each line the different names, instead of having only the last inserted name repeated all the times?
Reply
#4
As I mentioned, you need to put them into a collection (like a list), not assign them to a scalar str variable.

I would do something like this:

total_guests=int(input("Please type how many guests are in your group: "))
print(" ")

guest_names = []
for idx in range(total_guests):
    guest_names.append(input((f"Guest {idx+1}, Please write your name here: ")))
    print(" ")

for guest_number, guest_name in enumerate(guest_names, start=1):
    print(f"Hello {guest_name}, you are person {guest_number} to check in today")
Paulman likes this post
Reply
#5
That's a great solution. Now I see where I was messing up.
Many thanks for your help, really appreciated!!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Multiple for loops selenium webdriver KRS 4 2,508 Mar-10-2020, 03:31 PM
Last Post: KRS
  Multiprocessing using on multiple for loops Staph 3 2,522 Jul-28-2019, 07:24 AM
Last Post: ThomasL

Forum Jump:

User Panel Messages

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