Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
advice
#4
buran and ichabod801 provided great examples how to improve code.

I would suggest to add input validation mimicking real world scenarios. Currently, if user enters float, letter etc which can't be converted to int program will throw ValueError.

One possible way:

def validate(request):
    """Return allowed input in range 0-9"""
    
    allowed = range(10)
    
    m = ('Expected integer in range 0 - 9 '
         'but input was')
     
    while True:
        answer = input(request)
        try:
            answer = int(answer)
            if answer in allowed:
                return answer
         
        except ValueError:
            print(f'{m} {answer}')
             
        else:
            print(f'{m} {answer}')
Then you can write in game code:

pick = validate('Pick number between 0 and 9: ')
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Messages In This Thread
advice - by Sonoffox - Jan-04-2019, 01:44 PM
RE: advice - by ichabod801 - Jan-04-2019, 01:59 PM
RE: advice - by buran - Jan-04-2019, 02:06 PM
RE: advice - by perfringo - Jan-04-2019, 02:36 PM

Forum Jump:

User Panel Messages

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