Python Forum
Improvements for first script?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Improvements for first script?
#3
Thanks for the tips. Thumbs Up Didn't know about the style guide - Heading over to there ASAP.

For not using uppercase letters, then, measUnits would become meas_units.

I appreciate your comment about not needing to use "meas" everywhere, but I have reading issues and it helps me follow what's going on :)

I will also check out "f-strings". Are they related to g-strings Tongue

EDIT - updated script:

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

def main():
    # Set engineering units and range
    meas_units = input("Enter the units of measurement: ")

    # Input minimum value of measurement range
    while True:
        try:
            meas_min = float(input("Enter the minimum measurement value: "))
        except ValueError:
            print("Must be a numeric value...")
            continue
        else:
            break
    
    meas_min2 = abs(float(meas_min)) # removes "minus" bit

    # Input maximum value of measurement range
    while True:
        try:
            meas_max = float(input("Enter the maximum measurement value: "))
        except ValueError:
            print("Must be a numeric value...")
            continue
        else:
            break

    # Display measurement range
    meas_range = float(meas_min2) + float(meas_max)
    print(f"Measurement range is {meas_min} to {meas_max} {meas_units}")
    
    # Input measured value
    while True:
        try:
            raw_value = float(input("Input mV value: "))
        except ValueError:
            print("Must be a numeric value...")
            continue
        else:
            break

    # Calculate measured value
    step1 = float(raw_value) - 400
    step2 = step1 / 1600
    meas_value = (step2 * meas_range) - meas_min2
    output = round(meas_value,2)
    print("Measured value is {} {}".format(output, meas_units))

    restart = input("Do you want to convert another value? Y/N: ")
    print("")
    if restart.lower() == 'y':
        main()
    else:
        print("Goodbye")

main()
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