In this little example, I am showing how to make a simple game with python, because I think that it is the most effective way to learn a language, to do something that has a real application and that is very simple. You need to learn the basic, first of all, and after that you are ready to do more difficult stuff. I think it’s obvious, but it is not always easy to find the right path at the right pace.
So, here is the code. Below you find the live video making of the code, all improvised and rough.
from random import random from random import randint from random import randrange def rnd(): guess = [] for i in range(3): n = randrange(1, 10) guess.append(n) return guess a = rnd() a = [str(x) for x in a] a = "".join(a) score = 10 for t in range(10): print("\nTry n." + str(t+1)) print("=======") print("What were the numbers?") answer = input("> ") if answer == a: print("\n***********\n") print("Good, you guessed") print("Your score is " + str(score)) break else: count = 0 pos = 0 for n, i in enumerate(a): if a[n] == answer[n]: count += 1 elif a[n] in answer: pos += 1 print("_______") print(str(count) + " numbers right") print(str(pos) + " numbers" + " are not in the right position") score -= 1 print("The solution was:" + a) print("Your last try was:" + answer)
This is the output of a game
Try n.1 ======= What were the numbers? > 123 _______ 0 numbers right 1 numbers are not in the right position Try n.2 ======= What were the numbers? > 245 _______ 1 numbers right 1 numbers are not in the right position Try n.3 ======= What were the numbers? > 267 _______ 1 numbers right 0 numbers are not in the right position Try n.4 ======= What were the numbers? > 264 _______ 2 numbers right 0 numbers are not in the right position Try n.5 ======= What were the numbers? > 274 _______ 2 numbers right 0 numbers are not in the right position Try n.6 ======= What were the numbers? > 284 _______ 2 numbers right 0 numbers are not in the right position Try n.7 ======= What were the numbers? > 294 *********** Good, you guessed Your score is 4 The solution was:294 Your last try was:294 >>>
The video
Subscribe to the newsletter for updates
Tkinter templates
My youtube channel
Twitter: @pythonprogrammi - python_pygame