Total Pageviews

Monday, October 1, 2012

Ohm's Law Practice Program

For school, I had to learn Ohm's Law, and how to calculate it. So to help in my learning adventure, I created a quick program in Python 2.7 so I can practice. It's nothing fancy. Type in Q or q to quit.

import random;

stuff = " I " , " V ", " R ";
X = 0;
while ( X != 1):
    A1 = random.randint(0,2);
    A2 = random.randint(0,2);
    while ( A1 == A2 ):
        A2 = random.randint(0,2);
    B1 = str(random.randint(1,20));
    B2 = str(random.randint(1,20));
    print(stuff[A1] + B1 + " &" + stuff[A2] + B2+ "\n");
    inp = str(raw_input("Answer?") );
    if ( inp == "Q" or inp ==  "q"):
         X = 1;
    dip =0.;
    if (A1 == 0 and A2 == 1):
        dip = float(B2) / float(B1) ;
        print ( "Your answer: " + inp + " || Correct answer: R = " + str(dip) );
        print(stuff[A2] +" /" + stuff[A1]+ "\n");
    elif (A1 == 0 and A2 == 2):
        dip = float(B2) * float(B1) ;
        print ( "Your answer: " + inp + "|| Correct answer: V = " + str(dip) );
        print(stuff[A1] +" *" + stuff[A2] + "\n");
    elif (A1 == 1 and A2 == 0):
        dip = float(B2) / float(B1) ;
        print ( "Your answer: " + inp + "|| Correct answer: R = " + str(dip) );
        print(stuff[A2] +" /" + stuff[A1]);
    elif (A1 == 1 and A2 == 2):
        dip = float(B1) / float(B2) ;
        print ( "Your answer: " + inp + "|| Correct answer: I = " + str(dip) );
        print(stuff[A1] +" /" + stuff[A2]+ "\n");
    elif (A1 == 2 and A2 == 0):
        dip = float(B2) * float(B1) ;
        print ( "Your answer: " + inp + "|| Correct answer: V = " + str(dip) );
        print(stuff[A1] +" *" + stuff[A2]+ "\n");
    elif (A1 == 2 and A2 == 1):
        dip = float(B2) / float(B1) ;
        print ( "Your answer: " + inp + "|| Correct answer: I = " + str(dip) );
        print(stuff[A2] +" /" + stuff[A1]+ "\n");

No comments:

Post a Comment