patternpythonMinor
Electricity Calculator
Viewed 0 times
calculatorelectricitystackoverflow
Problem
This is a calculator for Ohm's Law and Power Law. It works but I'd like some suggestions on making the code look a little cleaner, or run more efficiently.
```
# ohm's law calculator
#These functions perform ohm's law and power law calculations based on variables from user input.
def voltage(i, r):
return i * r
def current(r, v):
return r / v
def resistance(i, v):
return i / v
def power(v, i):
return v * i
def pvolt(w, i):
return w / i
def pcurrent(w, v):
return w / v
# First user interface menu. let's user choose between ohm's law and power law calculations.
print "Ohm's Law Calculations:"
print "1. Voltage"
print "2. Current"
print "3. Resistance"
print "Power Law Calculations:"
print "4. Wattage"
print "5. Voltage"
print "6. Current"
#Line 48 is taking the user's menu choice and passing it to the "choice" options below.
choice = raw_input(">>> ")
#These if statements are calling the functions.
#This gives values to "i" "v" "w" and "r" from user input as floating point numbers the print line calls the "voltage" function and prints out the results of its calculation using the "format" function which automatically displays the needed decimal places.
if choice == "1":
i=float(raw_input("Enter current:"))
r=float(raw_input("Enter resistance:"))
print "{0} volts" .format(voltage(i, r))
elif choice == "2":
r=float(raw_input("Enter resistance:"))
v=float(raw_input("Enter voltage:"))
print "{0} amps" .format(current(r, v))
elif choice == "3":
i=float(raw_input("Enter current:"))
v=float(raw_input("Enter voltage:"))
print "{0} ohms" .format(resistance(i, v))
elif choice == "4":
i=float(raw_input("Enter current:"))
v=float(raw_input("Enter voltage:"))
print "{0} watts" .format(power(i, v))
#Line 82 solves voltage according to power law
elif choice == "5":
w=float(raw_input("Enter wattage:"))
i=float(raw_input("Enter current:"))
print "{0} volts" .format(pvolt(w, i
```
# ohm's law calculator
#These functions perform ohm's law and power law calculations based on variables from user input.
def voltage(i, r):
return i * r
def current(r, v):
return r / v
def resistance(i, v):
return i / v
def power(v, i):
return v * i
def pvolt(w, i):
return w / i
def pcurrent(w, v):
return w / v
# First user interface menu. let's user choose between ohm's law and power law calculations.
print "Ohm's Law Calculations:"
print "1. Voltage"
print "2. Current"
print "3. Resistance"
print "Power Law Calculations:"
print "4. Wattage"
print "5. Voltage"
print "6. Current"
#Line 48 is taking the user's menu choice and passing it to the "choice" options below.
choice = raw_input(">>> ")
#These if statements are calling the functions.
#This gives values to "i" "v" "w" and "r" from user input as floating point numbers the print line calls the "voltage" function and prints out the results of its calculation using the "format" function which automatically displays the needed decimal places.
if choice == "1":
i=float(raw_input("Enter current:"))
r=float(raw_input("Enter resistance:"))
print "{0} volts" .format(voltage(i, r))
elif choice == "2":
r=float(raw_input("Enter resistance:"))
v=float(raw_input("Enter voltage:"))
print "{0} amps" .format(current(r, v))
elif choice == "3":
i=float(raw_input("Enter current:"))
v=float(raw_input("Enter voltage:"))
print "{0} ohms" .format(resistance(i, v))
elif choice == "4":
i=float(raw_input("Enter current:"))
v=float(raw_input("Enter voltage:"))
print "{0} watts" .format(power(i, v))
#Line 82 solves voltage according to power law
elif choice == "5":
w=float(raw_input("Enter wattage:"))
i=float(raw_input("Enter current:"))
print "{0} volts" .format(pvolt(w, i
Solution
You should think more about how you arrange and style your code. Additionally, you should be aware of DRY methodology and keep from repeating code wherever possible.
I made a couple of modifications to your code. Primarily, I cleaned up the arrangement and styling. After that, I added an Ohm class - which places all of your Ohm related functions as methods (again for code cleanliness):
After that, I changed how you output the block of text from a bunch of print statements, to 1 multiline print statement:
After that, I added a call to
After that, we continue with an if/else block which calls the function according to the user input. Our i/v/r/w should already be defined in the block above. One major change here is that we are no longer printing directly from here. Rather we are setting the output in an
Finally, we check if
Lastly, here is all of the code put together (and a REPL):
```
"""
Ohm's law calculator
"""
"""
This class contains functions which perform Ohm's law and power
law calculations based on variables from user input
"""
class Ohm():
@staticmethod
def voltage(i, r): return i * r
@staticmethod
def current(r, v): return r / v
@staticmethod
def resistance(i, v): return i / v
@staticmethod
def power(v, i): return v * i
@staticmethod
def pvolt(w, i): return w / i
@staticmethod
def pcurrent(w, v): return w / v
# First user interface menu. let's user choose between ohm's law and power law calculations.
print """Ohm's Law Calculations:
1. Voltage
2. Current
3. Resistance
Power Law Calculations:
4. Wattage
5. Voltage
6. Current
"""
# Grab the user input
choice = int(raw_input(">>> "))
# Initialize our Ohm class
Ohm = Ohm()
### Since we're asking for the current in multiple locations, simplify it
if choice in [1, 3, 4, 5]:
i = float(raw_input("Enter current:"))
### Again, since we're asking for voltage in multiple locations, simplify it
if choice in [2, 3, 4, 6]:
v = float(raw_input("Enter voltage:"))
### Simplifying input for resistance
if choice in [1, 2]:
r = float(raw_input("Enter resistance:"))
## And lastly, simplifying for wattage
if choice in [5, 6]:
w = float(raw_input("Enter wattage:"))
output = None
if choice == 1:
output = "{0} volts".format(Ohm.voltage(i, r))
elif choice == 2:
output = "{0} amps".format(Ohm.current(r, v))
elif choice == 3:
output = "{0} ohms".format(Ohm.resistance(i, v))
elif choice == 4:
output = "{0} watts".format(Ohm.power(i, v))
elif choice == 5:
output = "{0} volts".f
I made a couple of modifications to your code. Primarily, I cleaned up the arrangement and styling. After that, I added an Ohm class - which places all of your Ohm related functions as methods (again for code cleanliness):
"""
This class contains functions which perform Ohm's law and power
law calculations based on variables from user input
"""
class Ohm():
@staticmethod
def voltage(i, r): return i * r
@staticmethod
def current(r, v): return r / v
@staticmethod
def resistance(i, v): return i / v
@staticmethod
def power(v, i): return v * i
@staticmethod
def pvolt(w, i): return w / i
@staticmethod
def pcurrent(w, v): return w / vAfter that, I changed how you output the block of text from a bunch of print statements, to 1 multiline print statement:
# First user interface menu. let's user choose between ohm's law and power law calculations.
print """Ohm's Law Calculations:
1. Voltage
2. Current
3. Resistance
Power Law Calculations:
4. Wattage
5. Voltage
6. Current
"""After that, I added a call to
int() on the choice = raw_input(...) call so that all input is shunted to an integer. After that, we initialize our Ohm class to an Ohm variable. Then, instead of having multiple definitions for i, r, v, and w - we ask if the choice is in a list, define i/r/v/w. This lessens the amount of duplicated code:### Since we're asking for the current in multiple locations, simplify it
if choice in [1, 3, 4, 5]:
i = float(raw_input("Enter current:"))
### Again, since we're asking for voltage in multiple locations, simplify it
if choice in [2, 3, 4, 6]:
v = float(raw_input("Enter voltage:"))
### Simplifying input for resistance
if choice in [1, 2]:
r = float(raw_input("Enter resistance:"))
## And lastly, simplifying for wattage
if choice in [5, 6]:
w = float(raw_input("Enter wattage:"))After that, we continue with an if/else block which calls the function according to the user input. Our i/v/r/w should already be defined in the block above. One major change here is that we are no longer printing directly from here. Rather we are setting the output in an
output variable, which we later use to print. output = None
if choice == 1:
output = "{0} volts".format(Ohm.voltage(i, r))
elif choice == 2:
output = "{0} amps".format(Ohm.current(r, v))
elif choice == 3:
output = "{0} ohms".format(Ohm.resistance(i, v))
elif choice == 4:
output = "{0} watts".format(Ohm.power(i, v))
elif choice == 5:
output = "{0} volts".format(Ohm.pvolt(w, i))
elif choice == 6:
output = "{0} amps".format(Ohm.pcurrent(w, v))Finally, we check if
output variable is None. If it is not None, then we print it, otherwise we print an error. As for your 'funny' error, remember that error messages should be informative (ie, should tell the end user where the error occurred and/or why it occurred):if output is not None:
print output
else:
print "Invalid input values, please try again"Lastly, here is all of the code put together (and a REPL):
```
"""
Ohm's law calculator
"""
"""
This class contains functions which perform Ohm's law and power
law calculations based on variables from user input
"""
class Ohm():
@staticmethod
def voltage(i, r): return i * r
@staticmethod
def current(r, v): return r / v
@staticmethod
def resistance(i, v): return i / v
@staticmethod
def power(v, i): return v * i
@staticmethod
def pvolt(w, i): return w / i
@staticmethod
def pcurrent(w, v): return w / v
# First user interface menu. let's user choose between ohm's law and power law calculations.
print """Ohm's Law Calculations:
1. Voltage
2. Current
3. Resistance
Power Law Calculations:
4. Wattage
5. Voltage
6. Current
"""
# Grab the user input
choice = int(raw_input(">>> "))
# Initialize our Ohm class
Ohm = Ohm()
### Since we're asking for the current in multiple locations, simplify it
if choice in [1, 3, 4, 5]:
i = float(raw_input("Enter current:"))
### Again, since we're asking for voltage in multiple locations, simplify it
if choice in [2, 3, 4, 6]:
v = float(raw_input("Enter voltage:"))
### Simplifying input for resistance
if choice in [1, 2]:
r = float(raw_input("Enter resistance:"))
## And lastly, simplifying for wattage
if choice in [5, 6]:
w = float(raw_input("Enter wattage:"))
output = None
if choice == 1:
output = "{0} volts".format(Ohm.voltage(i, r))
elif choice == 2:
output = "{0} amps".format(Ohm.current(r, v))
elif choice == 3:
output = "{0} ohms".format(Ohm.resistance(i, v))
elif choice == 4:
output = "{0} watts".format(Ohm.power(i, v))
elif choice == 5:
output = "{0} volts".f
Code Snippets
"""
This class contains functions which perform Ohm's law and power
law calculations based on variables from user input
"""
class Ohm():
@staticmethod
def voltage(i, r): return i * r
@staticmethod
def current(r, v): return r / v
@staticmethod
def resistance(i, v): return i / v
@staticmethod
def power(v, i): return v * i
@staticmethod
def pvolt(w, i): return w / i
@staticmethod
def pcurrent(w, v): return w / v# First user interface menu. let's user choose between ohm's law and power law calculations.
print """Ohm's Law Calculations:
1. Voltage
2. Current
3. Resistance
Power Law Calculations:
4. Wattage
5. Voltage
6. Current
"""### Since we're asking for the current in multiple locations, simplify it
if choice in [1, 3, 4, 5]:
i = float(raw_input("Enter current:"))
### Again, since we're asking for voltage in multiple locations, simplify it
if choice in [2, 3, 4, 6]:
v = float(raw_input("Enter voltage:"))
### Simplifying input for resistance
if choice in [1, 2]:
r = float(raw_input("Enter resistance:"))
## And lastly, simplifying for wattage
if choice in [5, 6]:
w = float(raw_input("Enter wattage:"))output = None
if choice == 1:
output = "{0} volts".format(Ohm.voltage(i, r))
elif choice == 2:
output = "{0} amps".format(Ohm.current(r, v))
elif choice == 3:
output = "{0} ohms".format(Ohm.resistance(i, v))
elif choice == 4:
output = "{0} watts".format(Ohm.power(i, v))
elif choice == 5:
output = "{0} volts".format(Ohm.pvolt(w, i))
elif choice == 6:
output = "{0} amps".format(Ohm.pcurrent(w, v))if output is not None:
print output
else:
print "Invalid input values, please try again"Context
StackExchange Code Review Q#63178, answer score: 4
Revisions (0)
No revisions yet.