-4
import random 

def roll(sides=6):
    num_rolled=randomint(1,sides)
    return num_rolled

def dice_game():
    sides = 6
    while True:
        roll_again = input("Ready to roll? Enter=ROLL. Q=Quit.")
        if roll_again.lower() != "q":
            num_rolled = roll(sides)
            print("You rolled a ", num_rolled)
        else: 
            rolling = False 
    print("Thanks for playing.")
dice_game()

when I try to run it through the command line on my local machine i get the following error:

Traceback (most recent call last):

File "SimpleDiceRollingSimulation.py", line 17, in <module>
    dice_game()

File "SimpleDiceRollingSimulation.py", line 10, in dice_game
    roll_again = input("Ready to roll? Enter=ROLL. Q=Quit.")

File "<string>", line 1, in <module>
NameError: name 'q' is not defined
Sayse
  • 42,633
  • 14
  • 77
  • 146
Austin
  • 1
  • 7
    If you're using Python 2, you should be using `raw_input`, not `input` – khelwood Oct 25 '17 at 14:56
  • Also, it should be `random.randint(1, sides)` for generating random number – yash Oct 25 '17 at 14:59
  • Several issues. `randomint` should be `random.randint`. Your loop is infinite, you set `rolling` to `False` but never test it, maybe you should just `break` if the user enters "q". – cdarke Oct 25 '17 at 14:59

1 Answers1

0

Try this

import random 

def roll(sides=6):
    num_rolled=random.randint(1,sides)
    return num_rolled

def dice_game():
    sides = 6
    while True:
        roll_again = input("Ready to roll? Enter=ROLL. Q=Quit.")
        if str(roll_again).lower() != "q":
            num_rolled = roll(sides)
            print("You rolled a ", num_rolled)
        else: 
            #rolling = False 
            break
    print("Thanks for playing.")
dice_game()
Sandeep Lade
  • 1,865
  • 2
  • 13
  • 23
  • And `raw_input()` if current python version is 2+ instead 3+. – LenglBoy Oct 25 '17 at 15:01
  • Infinite loop, does not quit when user enters "q". The symptoms of the reported error (name 'q' is not defined) fit the commented scenario that Python 2 is being used, which this answer does not address. – cdarke Oct 25 '17 at 15:02
  • 1
    @cdarke: Added break in else , that should break from while once user enters q – Sandeep Lade Oct 25 '17 at 15:05