Python Forum
Improvements for first script?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Improvements for first script?
#4
Some observations.

There is the repetition of input validation. If such pattern emerges then it is clear that function should be used. Currently the code is not DRY (Don't Repeat Yourself). Simple validation function should be used, something along those lines:

def validate(request):
    while True:
        answer = input(request)
        try:
            return abs(float(answer))
        except ValueError:
            print(f'Expected numeric value but {answer!r} was entered')
Now you can use this or similar function when you need to validate user input to be numeric and absolute value. You can be 'clever' and use loop and unpacking while using it:

low, high = (validate(f'Enter the {boundary} measurement value: ') for boundary in ['minimum', 'maximum'])
alloydog likes this post
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
Improvements for first script? - by alloydog - Dec-27-2020, 03:57 PM
RE: Improvements for first script? - by perfringo - Dec-28-2020, 05:25 AM
RE: Improvements for first script? - by Mark17 - Dec-30-2020, 05:41 PM
RE: Improvements for first script? - by alloydog - Dec-28-2020, 07:55 AM
RE: Improvements for first script? - by perfringo - Dec-30-2020, 08:01 AM
RE: Improvements for first script? - by alloydog - Dec-30-2020, 09:25 AM
RE: Improvements for first script? - by DeaD_EyE - Dec-30-2020, 12:28 PM
RE: Improvements for first script? - by alloydog - Dec-30-2020, 05:28 PM
RE: Improvements for first script? - by alloydog - Jan-01-2021, 01:45 PM
RE: Improvements for first script? - by perfringo - Jan-01-2021, 02:50 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Any improvements Chuck_Norwich 2 2,710 Jul-14-2020, 05:15 AM
Last Post: Gamo
  [split] Any improvements Adamstiffman 1 1,928 May-31-2020, 06:16 AM
Last Post: pyzyx3qwerty
  Coding improvements chris_drak 2 37,409 May-02-2020, 11:39 AM
Last Post: chris_drak

Forum Jump:

User Panel Messages

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