#!/usr/bin/perl -w use strict; use warnings; print "I will calculate whatever\n"; print "you specify according to\n"; print "Ohm's law. What shall I\n"; print "calculate? Type either\n"; print "voltage, current, or\n"; print "resistance.\n"; my $i = 0; while($i == 0) { my $calculation = ; chomp $calculation; if($calculation =~ m/voltage/) { print "Type the current of the circuit.\n"; print "Add mA at the end of your answer\n"; print "if your answer is in milliAmps.\n"; my $current = ; chomp $current; print "Now type the resistance of the circuit.\n"; my $resistance = ; chomp $resistance; if($current =~ m/mA/) { $current =~ s/mA//; $current =~ s/^\s+//; $current =~ s/ //; $current /= 1000; } else { } my $voltage = $current * $resistance; print "Ohm's law is V = IR, so the voltage of\n"; print "the circuit is $voltage volts.\n"; $i++; } elsif($calculation =~ m/current/) { print "Type the resistance of the circuit.\n"; my $resistance = ; chomp $resistance; print "Now type the voltage of the circuit.\n"; my $voltage = ; chomp $voltage; my $current = $voltage / $resistance; chomp $current; my $otherCurrent = $current / 1000; chomp $otherCurrent; print "Ohm's law is V = IR, so the current of\n"; print "the circuit is $current amps,\n"; print "or $otherCurrent milliAmps.\n"; $i++; } elsif($calculation =~ m/resistance/) { print "Type the current of the circuit.\n"; print "Add mA at the end of your answer\n"; print "if your answer is in milliAmps.\n"; my $current = ; chomp $current; print "Now type the voltage of the circuit.\n"; my $voltage = ; chomp $voltage; if($current =~ m/mA/) { $current =~ s/mA//; $current =~ s/^\s+//; $current =~ s/ //; $current /= 1000; } else { } my $resistance = $voltage / $current; chomp $resistance; print "Ohm's law is V = IR, so the resistance of\n"; print "the circuit is $resistance ohms.\n"; $i++; } else { print "That is not a valid answer.\n"; print "Retype your answer.\n"; } }