learn-python/contrib/mini-projects/dice_roller.md

30 wiersze
1.4 KiB
Markdown
Czysty Zwykły widok Historia

2024-05-19 04:14:37 +00:00
Dice Roller
<br>
The aim of this project is to replicate a dice and generate a random number from the numbers 1 to 6.<br><br>
For this first we will import the random library which will help make random choices.
2024-05-13 15:40:18 +00:00
```
import random
def dice():
dice_no = random.choice([1,2,3,4,5,6])
return "You got " + str(dice_no)
2024-05-19 04:14:37 +00:00
```
The above snippet of code defines a function called "dice( )" which makes the random choice and returns the number that is generated.
```
2024-05-13 15:40:18 +00:00
def roll_dice():
print("Hey Guys, you will now roll a single dice using Python!")
while True:
start=input("Type \'k\' to roll the dice: ").lower()
if start != 'k':
print("Invalid input. Please try again.")
continue
print(dice())
roll_again = input("Do you want to reroll? (Yes/No): ").lower()
if roll_again != 'yes':
break
print("Thanks for rolling the dice.")
roll_dice()
```
2024-05-19 04:14:37 +00:00
The above code defines a function called "roll_dice( )" which interacts with the user.<br>
It prompts the user to give an input and if the input is k,the code proceeds further to generate a random number or gives the message of invalid input and asks the user to try again.<br>
After the dice has been rolled once, the function asks the user whether they want a reroll in the form of a yes or no question.The dice is rolled again if the user gives 'yes' as an answer and exits the code if the user replies with anything other than yes.