# Dice rolling game

I am new to programming and I tasked myself with the challenge of making a dice rolling game. It goes from 1 to 6 and it first asks you if you're interested in playing. Then it rolls. Then it spits out the number, and asks if you want to play again.

I got it to work, however, I can guarantee that it could be shorter. I am COMPLETELY stumped on this part. I would honestly like critique on how to clean up my programming. I know I'm brand new, but I would like to learn efficiency as well as the basics if possible.

I programmed in Python version 3.

import random
game_start = input("Would you like to roll the dice?")

def dice_roll():
print("Your number is: " + str(random.randint(1,6)))
global play_again
play_again = input("Would you like to play again?")

if game_start == "yes":
dice_roll()
while play_again == "yes":
dice_roll()
elif game_start == "no":
print("Game Over")
else:
print("Input not recognized")

• the if statements could be added inside the dice_roll() function, set up a while True: loop and put the dice roll part there, then if the if statements are true, just return – José Garcia Aug 7 '17 at 1:10
• @JoséGarcia okay, I'm not sure if I'm understanding what you mean. Could you perhaps lay it out? Because I am not entirely sure where youre going with this. Thank you so much for replying though. – thesolidmoose Aug 7 '17 at 1:17
• using the global, is something you should avoid, there are lots of blogs explaining problems relate to the use global variables – diek Aug 7 '17 at 1:26
• @diek thank you! I will avoid using globals – thesolidmoose Aug 7 '17 at 2:45

import random

def dice_roll():
while True:
print("Your number is: " + str(random.randint(1,6)))
play_again = input("Would you like to play again? ")
while play_again != 'yes':
if play_again == 'no':
return print("Game Over")
else:
print("Input not recognized")
play_again = input("Would you like to play again? ")

def main():
game_start = input("Would you like to roll the dice?")
if game_start == 'yes':
dice_roll()
else:

if __name__ == '__main__':
main()


the while inside dice_roll() will roll the dice as long as you want to play again. I made your inicial input part of main(), so if you want to use this program from another one, it doesn't do anything, but you still get to use the dice_roll() function.

• Okay! So everything that you mentioned makes sense to me now, except the second While loop. I completely understand why its in there, however I don't know why it is: while play_again != "yes" – thesolidmoose Aug 7 '17 at 2:31
• Why is it that you made it "Not equal" to yes? That is where I am lost. Thank you!!! – thesolidmoose Aug 7 '17 at 2:32
• @thesolidmoose Because if you would input 'yes', would just simply skip the whole 2nd while and print again your number. If it's not 'yes' ('no' or some gibberish), it would enter the second while, then it would check if it was 'no' to end the game, or gibberish to ask you to play again. – bitcell Aug 7 '17 at 7:07
• I'd rename dice_roll. The name implies that is does exactly that. Wouldn't expect it to do anything related to IO. How about dice_game? Also, right now there is not much code. But if he extends this by something, like rolling several dice and then rerolling some of them to create some patterns et cetera, the code will increase and should be split in the code that maintains the while loop and the code that governs a single turn. Since this is most likely what he will continue to do, I'd separate it already, even if this will result in more code right now. – Aziuth Aug 7 '17 at 15:33

Perhaps you could put it all in one loop. Maybe roll it all into the function or put the function code where the 'while' loop is.

Something like: if yes, then print("Your number is: " + str(random.randint(1,6)))

Also, you might not need a "no" option at all. It could just have what you wrote for "yes" and have anything else be "game over". But I see that maybe the "input not recognized" is a part of your task.

Merge game_start and play_again into one variable - note they already mean almost the same thing, and you never need both at the same time. So call it play or wants_to_play or keep_playing etc.

That will allow you to collapse some of your repeated if statements and loops.

Note also that dice_roll doesn't just roll the dice - it also asks if you want to play again. Don't do that - a function should just do one thing, not two. Maybe dice_roll should just do print("Your number is: " + str(random.randint(1,6))), or, in fact, maybe it should just return random.randint(1,6) and let other code worry about display - imagine if you wanted to make a GUI - try to keep the UI code from spreading everywhere.

so think how the game should work (this is not python):

wants_to_play = ask "do you want to play"
while wants_to_play
roll dice, display roll

P.S. maybe ask could be a function, that handles the bad input etc, and converts the answer to true/false.