Skip to content

Instantly share code, notes, and snippets.

@mpawliuk
Last active June 14, 2018 23:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mpawliuk/a6e8508a3bbad19478b1f5a168cc832e to your computer and use it in GitHub Desktop.
Save mpawliuk/a6e8508a3bbad19478b1f5a168cc832e to your computer and use it in GitHub Desktop.
This generates a game where there is an algebraic expression whose operations are hidden. It's from this Reddit post: https://redd.it/8r0a66
import random
def generate_game(largest_number, length, difficulty):
"""Produce an example of the number game.
- largest_number is the largest possible number that can appear
- length is how many numbers will appear in the problem
- difficulty = 0,1,2. 0 has just addition/subtraction, 1 introduces multiplication, 2 has +,-,*,/
"""
# Set difficulty
operations = ['+', '-']
if difficulty > 0:
operations.append('*')
if difficulty > 1:
operations.append('/')
# Add numbers and operations
game = [str(random.randint(0,largest_number))]
for i in range(length-1):
game.append(random.choice(operations))
game.append(str(random.randint(1,largest_number)))
# Make hidden game. Remove the operations.
hidden_game = ""
for i in range(len(game)):
if i % 2 == 0:
hidden_game += str(game[i])
else:
hidden_game += "_"
hidden_game += " = " + str(eval(''.join(game)))
print hidden_game
x = raw_input("Click to see a solution.")
print ''.join(game) + " = " + str(eval(''.join(game)))
print '----------'
return None
# Play!
s = int(raw_input("What's the largest number you want to appear? (Enter a whole number larger than 0)"))
l = int(raw_input("How many numbers do you want to appear? (Enter 1,2, ..., 10)"))
d = int(raw_input("What difficulty do you want? 0 = + and - only, 1 contains multiplication, 2 contains all operations."))
for i in range(int(raw_input("How many games do you want to play? (Enter 1,2, ..., 10)"))):
generate_game(s, l, d)
print "Thank you for playing!"
@mpawliuk
Copy link
Author

Fixed the order of operations.

@mpawliuk
Copy link
Author

I'm using eval() to find the solution now. This avoids having to hardcode the order of operations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment