0

I'm trying to repeat a code that asks the user for a name, and thereafter asks for a new name. If the user writes a number, the program should ask for a new name. If the user types 'quit' the program should print how many names the user has entered.

So far I've solved it with a while-loop, but would like to do it WITHOUT using a while-loop and still keep prompting the user for new names.

participants=[]
count=0

while True:
    user_name=input("Course participant name: ")
    if user_name == "quit":
        print("Number of participants: ", count)
    elif user_name.isdigit():
        continue
    elif user_name.isalpha():
        participants.append(user_name)
        count+=1
    else:
        print("Invalid input")
        break

Any suggestions?

Krishna
  • 924
  • 1
  • 7
  • 28
Madman12
  • 37
  • 4

3 Answers3

3

You could use recursion:

def ask(participants):
    user_name = input("Course participant name: ")
    if user_name == "quit":
        print("Number of participants: ", len(participants))
    elif user_name.isdigit():
        ask(participants)
    elif user_name.isalpha():
        participants.append(user_name)
        ask(participants)
    else:
        print("Invalid input")
        return

Instead of looping, you go deeper and deeper into the call stack. No need to track count separately because it is already encoded in the length of participants.

piripiri
  • 1,925
  • 2
  • 18
  • 35
  • True, see e.g.: https://stackoverflow.com/questions/3323001/what-is-the-maximum-recursion-depth-in-python-and-how-to-increase-it Given that a user has to press keys for each level it will probably be fine but for practical purposes I would go with the original while-loop. – piripiri Nov 28 '18 at 10:22
0

If you are looking for solution using for loop then you can do as follow:

participants=[]
count=0
from itertools import cycle
for i in cycle(range(0, 1)):
    user_name=input("Course participant name: ")
    if user_name == "quit":
        print("Number of participants: ", count)
    elif user_name.isdigit():
        continue
    elif user_name.isalpha():
         participants.append(user_name)
         count+=1
    else:
         print("Invalid input")
         break
piet.t
  • 11,718
  • 21
  • 43
  • 52
BishalG
  • 1,414
  • 13
  • 24
0

This is a slightly tricky way to do it, by using the participants list itself and providing an initial element (to start the loop), which will be deleted in the end. The loop will keep going as long as there is a name being inputted, because the participants list grows at every iteration.

participants=["Start"] # insert initial element, to start the loop
count=0

for i in participants:
    user_name=input("Course participant name: ")
    if user_name == "quit":
        print("Number of participants: ", count)
    elif user_name.isdigit():
        continue
    elif user_name.isalpha():
        participants.append(user_name)
        count+=1
    else:
        print("Invalid input")
        break

del participants[0] # remove initial element
Andreas
  • 2,455
  • 10
  • 21
  • 24