Skip to content

Instantly share code, notes, and snippets.

@jsam
Created January 15, 2014 18:16
Show Gist options
  • Save jsam/8441387 to your computer and use it in GitHub Desktop.
Save jsam/8441387 to your computer and use it in GitHub Desktop.
bc
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