Created
January 15, 2014 18:16
-
-
Save jsam/8441387 to your computer and use it in GitHub Desktop.
bc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random, subprocess | |
def start_game(): | |
operations = ['+', '-', '*', '/'] | |
for i in range(10): | |
n1 = random.randint(0, 100) | |
n2 = random.randint(0, 100) | |
operation = operations[random.randint(0, 3)] | |
sol = raw_input("{} {} {} = ".format(n1, operation, n2)) | |
p = subprocess.Popen("bc", stdin=subprocess.PIPE, | |
stdout=subprocess.PIPE, | |
stderr=subprocess.PIPE) | |
out, err = p.communicate("{} {} {}\n".format(n1, operation, n2)) | |
if err: | |
print("Parse Error") | |
continue | |
if (sol + "\n") == out: | |
print("Correct!\n") | |
else: | |
print("Wrong asnwer! Correct answer is {}".format(out)) | |
if __name__ == "__main__": | |
start_game() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment