0

I have simple code:

def simpleMethod():
    try:
        list_to_app = []                                                                                  
        number_of_a = input('\nHow many a you want to create? ')
        number_of_a = int(number_of_a)
        for i in range(number_of_a):                                                           
            user_a = input('\nPlease type here some track: ')
            list_to_app.append(user_a.lower())                                          
    except ValueError:
        stderr.write('Type error - please provide an integer character')
simpleMethod()

I do not want to use something like while True:... How to make some loop (I think while will be fine in this case) for this kind of flow:

  1. user types non-integer
  2. program shows Type error - please provide an integer character
  3. program goes back to 1. step

This is simple method but I'm stuck.

2
  • Why do you not want to use a while loop? That is indeed the easiest way to do this. Commented Mar 9, 2021 at 16:18
  • Why not "while True"? Is this some homework with questionable restrictions? Commented Mar 9, 2021 at 16:19

1 Answer 1

2

Add a while True loop to repeat the try block until it can successfully break. You probably also want to return the list you're building so that the caller can access it:

def simpleMethod():
    list_to_app = []  

    while True:
        try:
            number_of_a = input('\nHow many a you want to create? ')
            number_of_a = int(number_of_a)
            break
        except ValueError:
            stderr.write('Type error - please provide an integer character')

    for i in range(number_of_a):                                                           
        user_a = input('\nPlease type here some track: ')
        list_to_app.append(user_a.lower())

    return list_to_app  # no point building a list you don't return!                          

tracks = simpleMethod()

A simpler way to build a list is a comprehension -- you can actually put almost the entire function in a single comprehension statement within that while, since the exception will raise to the outer loop. You might also want to make the function name more descriptive:

from typing import List

def get_user_tracks() -> List[str]:
    """Prompt the user for a list of tracks."""
    while True:
        try:
            return [
                input('\nPlease type here some track: ') 
                for _ in range(int(input('\nHow many a you want to create? ')))
            ]
        except ValueError:
            stderr.write('Type error - please provide an integer character')

tracks = get_user_tracks()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.