From 955212378cd95fc4f7e7913dbb30d1b738ee5ece Mon Sep 17 00:00:00 2001 From: DanishVasdev <51286478+DanishVasdev@users.noreply.github.com> Date: Thu, 20 Jan 2022 00:02:16 +0530 Subject: [PATCH] Added a python program example Hi, I have added an example python excercise in the Python folder. It is a basic implementation of game of nim. This is my first contribution of github so please do give feedback if I do something wrong or if something can be improved --- Python/game of nim.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 Python/game of nim.py diff --git a/Python/game of nim.py b/Python/game of nim.py new file mode 100644 index 0000000..074040f --- /dev/null +++ b/Python/game of nim.py @@ -0,0 +1,43 @@ +#game of nim + +""" +Rules +1.You take turns with the computer to take away 1/2 coins from a stack of coins +2.Player to take last coin wins +""" + +n=int(input("Enter number of coins in stack (>3):")) +print("OK! Lets Play!") +print("You get to go first ;)") +while n>0: + taken=int(input("How many do you want to take (1/2)? :")) + if taken not in range(1,3): + print("Not cool...play by the rules") + continue + n=n-taken + if n>0: #this if is only to check whether there are coins left for the computer + if n%3==0: + print("Ok..then i ll take...1!") + n=n-1 + elif n%3==1: + print("Ok..then i ll take...1!") + n=n-1 + else: + print("Ok..then i ll take...2!") + n=n-2 + print("Number of coins left:",n) + +if n==0: + print("Ha Ha! I win...better luck next time ;)") +else: + print("hmm...you win..") + + +""" +try and observe that whether you can win or not actually depends on the initial +number of coins you take :) + +to understand this better, think about what would happen if you could +only take 1 each? +""" +