I was trying to create a dice rolling program where the user enters how much a certain amount of dice they want to roll. But it is not working. What should I do?
from random import branding
repeat = True
while repeat:
amount = input('how many dice do you want to roll?')
for i in range(0, amount):
print("You rolled",randint(1,6))
print("Do you want to roll again?")
repeat = ("y" or "yes") in input().lower()
I am new to this language, but I love it and I bet you do too, hence I put together an idea based on your code, I certainly hope this answers your question. Keep coding bro, Python is great!
#First, you only need the random function to get the results you need :)
import random
#Let us start by getting the response from the user to begin
repeat = input('Would you like to roll the dice [y/n]?\n')
#As long as the user keeps saying yes, we will keep the loop
while repeat != 'n':
# How many dices does the user wants to roll, 2 ,3 ,4 ,5 who knows. let's ask!
amount = int(input('How many dices would you like to roll? \n'))
# Now let's roll each of those dices and get their results printed on the screen
for i in range(0, amount):
diceValue = random.randint(1, 6)
print(f"Dice {i+1} got a [{diceValue}] on this turn.")
#Now, let's confirm if the user still wants to continue playing.
repeat = input('\nWould you like to roll the dice [y/n]?\n')
# Now that the user quit the game, let' say thank you for playing
print('Thank you for playing this game, come back soon!')
# Happy Python Coding, buddy! I hope this answers your question.
On the 4th line, you just did input()
. However, you need to add an int()
function around the input.
This would be your code:
from random import *
repeat = True
while repeat:
amount = int(input('how many dice do you want to roll?'))
for i in range(0, amount):
print("You rolled",randint(1,6))
print("Do you want to roll again?")
repeat = ("y" or "yes") in input().lower()
Hope this helps!
You're importing branding
from the random
module, not randint
.
Use int()
when calling the input()
function, for the range()
function to work properly.
In the last line, repeat = ("y" or "yes") in input().lower()
, ("y" or "yes")
will evaluate to a boolean, and then your check will become something like True in input().lower()
which gives incorrect, and bizarre results. Use something like repeat = input().lower() in ("y", "yes")
instead.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.