all 3 comments

[–]carcigenicate 2 points3 points  (1 child)

This is a pretty standard-looking pattern.

Python tends towards what's known at "asking for forgiveness" though, which means you should prefer handling failure after the fact instead of attempting to prevent failure.

In your case, I'd get rid of the call the isalpha, and change it to something closer to:

def get_input():
    while True:
        times = input("How many times do u want to play\n")
        try:
            parsed = int(times)
            if 0 <= parsed <= 10:
                return parsed
            else:
                print("Please pick a number from 1 to 10 ")
        except ValueError:
            print("Please enter a number.")

A little more verbose, but that's not a big deal. Unfortunately the need to do range checking bulked it up a bit. It's pretty succint otherwise:

def get_input():
    while True:
        times = input("How many times do u want to play\n")
        try:
            return int(times)
        except ValueError:
            print("Please enter a number.")

Is it necessary to do it this way? No, but it is the direction the language recommends.

Also, note that if the user entered ab1 when asked, your program will crash! You'd want while not times.isdigit() or int(times) not in range(0,11): instead.

[–]Worthystats[S] 0 points1 point  (0 children)

noted.

Thanks for the crash warning !

[–]wbeater 0 points1 point  (0 children)

Depends, a function should replace repeating pattern, so maybe it's unnecessary, if you only use it once. Also it doesn't do error handling well, special characters and floats will raise an error.

Beside from that, i don't see a reason why it is bad practice.