Python Forum
Improvements for first script?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Improvements for first script?
#1
My way of learning is to think of something I need doing, then try a line, step by step, googling each error message and so on. I *don't* look for a complete script, that someone else has already done, to modify. I know it's re-inventing the wheel, but it works for me :)

Anyway, this is my first ever python script. It very simple, but it works. But, I'm just wondering if it is OK as it stands, or could it be a bit more elegant? I mean, would a grizzled old Python hack give a gentle nod of his head and say something like "Not bad, not bad. For a hardware engineer", or would he curl up in the corner, realising 2020 just got worse?...

EDIT: I know it needs things like making sure you can only input numeric values where they're required.

# Convert millivolts to engineering units
print ("4-20 mA to Engineering units convertor")
print ("")

def main():
    # Set engineering units and range
    measUnits = input("Enter the units of measurement: ")
    measMin = input("Enter the minimum measurement value: ")
    measMin2 = abs(float(measMin)) # removes "minus" bit
    measMax = input("Enter the maximum measurement value: ")
    measRange = float(measMin2) + float(measMax)
    print ("Measurement range is {} {}".format(measRange, measUnits))

    # Input measured value
    rawValue = input("Input mV value: ")

    # Calculate measured value
    step1 = float(rawValue) - 400
    step2 = step1 / 1600
    measValue = (step2 * measRange) - measMin2
    output = round(measValue,2)
    print ("Measured value is {} {}".format(output, measUnits))

main()

# Restart? yay or nay?
restart = input("Do you want to convert another value? Y/N: ")
if restart.lower() == 'y':
    main()
else:
    print ("Goodbye")
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