Tutorial: Create a dice game with python.

in GEMS5 years ago

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!

débutant post img.jpg

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.

won.PNG

The program:

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!

Capture.PNG

by @guliko.

Sort:  

This is a nice tutorial. The GitPlait community reward tutorials like this. If you wish, you could join our community.

https://hive.blog/trending/hive-103590

Cheers!

Thanks, I'm coming to see your community right away ;-)