Python Forum

Full Version: Multiple loops
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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.
Thanks, so how can I get for each line the different names, instead of having only the last inserted name repeated all the times?
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")
That's a great solution. Now I see where I was messing up.
Many thanks for your help, really appreciated!!