Skip to content

Instantly share code, notes, and snippets.

@TheoKlein
Last active December 25, 2020 13:59
Show Gist options
  • Select an option

  • Save TheoKlein/aeadb5dc6236b06eb020cd48a8e72cac to your computer and use it in GitHub Desktop.

Select an option

Save TheoKlein/aeadb5dc6236b06eb020cd48a8e72cac to your computer and use it in GitHub Desktop.
simple 1A2B game by python
# -*- coding: utf-8 -*-
import random
def gen_ans():
ans = []
while len(ans) != 4:
n = random.randrange(0, 10)
if not n in ans:
ans.append(n)
print("--debug only--")
print(ans)
return ans
def check_input(user_input):
numbers = user_input.split(' ')
if len(numbers) != 4:
return False
return True
def check_answer(ans, nums):
a, b = 0, 0
for n in nums:
if n in ans:
if nums.index(n) == ans.index(n):
a += 1
else:
b += 1
return a, b
def main():
answer = gen_ans()
guessing = True
while guessing:
user_input = raw_input("Give me 4 numbers: ")
if check_input(user_input):
numbers = user_input.split(' ')
numbers = map(int, numbers)
a, b = check_answer(answer, numbers)
print("\n%dA%dB" % (a, b))
if a == 4:
print("Nice job!")
guessing = False
else:
print("Keep trying!")
else:
print("\nYou need to guess 4 numbers between 0 ~ 9 and separated by space.")
print("For example: 1 2 3 4")
print("Please try again.")
if __name__ == "__main__":
print("Welcome to 1A2B")
print("You need to guess 4 numbers between 0 ~ 9 and separated by space.")
print("The numbers won't be repeated.")
main()
@mr-shrayan

mr-shrayan commented Dec 25, 2020

Copy link
Copy Markdown

Its outdated coz its made for python 2

https://gist.github.com/mr-shrayan/bc13c408544a2e27d3537a47d5a9b33f

Ive updated the code

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