1

I am working on a simple program that is basically just dice, I am using Thonny as my IDE and the program works fine in there but when I try and open the .py file the question comes up then I type something and the window just closes. Any help is appreciated.

import random
#-------------------------
print("Made by Thr i ving")
roll = input("Type roll to roll the dice: ")

if roll == 'roll':
    nums = ['1', '2', '3', '4', '5', '6']
    print("Your number is: " + random.choice(nums))
else:
    print("Try again.")
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Thr i ving
  • 11
  • 3
  • 2
    That's likely because the program immediately ended after printing out the number, closing the console window. Try putting another `input()` statement after everything (like `input("press enter to exit")`) and see if that achieves what you want. – metatoaster Aug 28 '18 at 02:48
  • Thank you, this fixed the issue i was having. – Thr i ving Aug 28 '18 at 02:53

1 Answers1

1

I am guessing you're expecting the code to ask again the user if the entered text is not exactly 'roll'.

If that's correct, I'd use a while loop to wait for the correct input. Until the word 'roll' is entered, the program will keep asking the user for a new input:

import random
#------------------------- print("Made by Thr i ving")
roll = ""

while roll != 'roll':

    roll = input("Type roll to roll the dice: ")

    if roll == 'roll':
        nums = ['1', '2', '3', '4', '5', '6']
        print("Your number is: " + random.choice(nums))
    else:
        print("Try again.")
kevh
  • 323
  • 2
  • 6