> I want to warn you that I am not an expert in programming, I discover and I share with you in order to make you take full advantage of my programs and progress together.
Welcome to this tutorial: How to create a dice game on python. It is very simple!
Presentation:
The game has 6 possible outcomes (like a 6-sided die). You must choose a number between 1 and 6 then a number will be generated randomly. If you have the same then you win otherwise you lose.
If I chose the number 2 and it generates the number 5 I lost, and if I chose the number 2 and it generates the number 2, I won.
[IMAGE: https://images.hive.blog/DQmPVoRE8eu7Y3EvpW8c6CafaHgko8ppugLow38643ZBebV/won.PNG]
The program:
[IMAGE: https://images.hive.blog/DQmfPwqh9CPssZdC4cmrBv3XC18bYxNrtN9krhwb8heRnZX/Capture.PNG]
from random import
print("Welcome to the dice game !")
print("Rules: Choose a number between 1 and 6, then press your 'Enter' key.")
print("A number will generate randomly and if you have the same as the generated number, you will win.")
number_choose = int(input("Chooser a number between 1 and 6."))
generate = randint(1, 6)
print("The number generated is: {}.".format(generate))
if number_choose is generate:
print("You won !")
else:
print("You lost!")
Explanations:
"from random import" : We import into "random" library: randint. Randint is used to generate a random number.
print("...") : "Print ()" is used to send a message to the console, we use it to give the result, to say the rules ...
"number_choose = int(input("Chooser a number between 1 and 6."))" : We will create the variable "number_choose" which will be equal to the response of "Chooser a number between 1 and 6.".
"generate = randint(1, 6)" : We will create the variable "generate" which will recover the number generated between 1 and 6 thanks to the randint function.
"print("The number generated is: {}.".format(generate))" : We announce the number that has been generated. ".format (generate)" will be used to replace the {} to put the value of the variable "generate" and therefore say the number selected.
"if number_choose is generate:
print("You won !")
else:
print("You lost!")" : If the number chosen ("number choose") is the same as the number generated ("generate"), then say "You won!" or otherwise "You lost!".
> The article is finished, thank you for taking the time to read my post and good luck to you <3
If you have any problems or questions, tell me!
[IMAGE: https://images.hive.blog/DQmcXzzgkJbFMju7baDtmwtbZ971XjN4UdVd2BV9KX2sbLX/Capture.PNG]
by @guliko.