Python Forum
Help with rerolling the dice
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with rerolling the dice
#1
Hi, so I made this programme which shows a different number when it runs(dice). The problem is when I type yes to : Do you want to roll again, it gives me the same random integer back even though the method is supposed to give me a different one. This is fixed however by manually rerunning the code. Is there a way that I could do this in the code?

import random

# a function where when it runs it prints a
# random number between 1 and 6
# then it should ask you if you want to roll again

dice = random.randint(1, 6)

# create a function that runs and prints
ask_again = True

def roll_dice():
    global ask_again
    print(dice)
    answer = input("Do you want to roll again?: \n")
    if answer == "no" or answer == "No":
        ask_again = False
    elif answer == "Yes" or answer == "yes":
        ask_again = True

while ask_again:
    roll_dice()
Reply
#2
You ask many times, but you actually "roll" only once in line 7.
The function "roll_dice" does not roll the dice.

Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#3
your line that picks a random number
dice = random.randint(1, 6)
is outside of the loop so it will always be the same value.
move this line into the loop.
Reply
#4
Of course! I'm kinda new to this. But when it's outside the loop the random integer gets assigened to the variable and stays the same. By putting it in the function the method reruns. Thanks!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Making a percentile dice roller and dice roller Fixer243 2 3,199 Sep-30-2018, 12:18 PM
Last Post: gruntfutuk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020