Skip to content

Instantly share code, notes, and snippets.

@svend
Created December 3, 2017 07:56
Show Gist options
  • Select an option

  • Save svend/31f920cb18df9f938487755198df5f64 to your computer and use it in GitHub Desktop.

Select an option

Save svend/31f920cb18df9f938487755198df5f64 to your computer and use it in GitHub Desktop.
Harper's sparkle game in Python
#!/usr/bin/env python3
MAX = 50
def get_sparkle():
"""Gets the sparkle number from the player"""
while True:
try:
n = int(input('What is the sparkle number? '))
except (NameError, ValueError):
# NameError for Python 2 compatibility
print("That is not a number.")
else:
if 0 <= n < 10:
return n
else:
print("Sparkle number must be between 0 and 9.")
def is_sparkle(n, sparkle):
"""Returns True if *n* is sparkle a number"""
# Use string matching instead of math:
# str(i).endswith(str(num))
return n % 10 == sparkle
if __name__ == '__main__':
sparkle = get_sparkle()
for i in range(1, MAX + 1):
if is_sparkle(i, sparkle):
print("SPARKLE!")
else:
print(i)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment