骰子游戏中没有定义变量

时间:2017-10-25 14:53:27

标签: python

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()

当我尝试通过本地计算机上的命令行运行它时,我收到以下错误:

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

1 个答案:

答案 0 :(得分:0)

试试这个

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()