Python骰子游戏错误

时间:2014-10-11 00:13:50

标签: python dice

[解决]这是一个愚蠢的错字。遗憾。

我目前正在学习python而且我遇到了一些错误,如果你能指出我如何修复它们以及为什么它们会很好,因为我通过项目的试错来了解像这些。 错误;

Would you like one or two die?2
Traceback (most recent call last):
  File "diceRoller.py", line 34, in <module>
    rollDice2();
  File "diceRoller.py", line 18, in rollDice2
    result = random.randrage(2,13)
AttributeError: 'module' object has no attribute 'randrage'

我的代码;

import random
import time

numDice = input("Would you like one or two die?")
if (numDice == 1):
    rollDice1();
else:
    rollDice2();

def rollDice1():
    result = random.randrange(1,7)
    print ("It landed on..")
    time.sleep(1)
    print(result)
    try:
        answer = input("would you like to play again? [y/n]")
    except:
        pass
    if answer in ('y','Y'):
        return True
    return False

def rollDice2():        
    result = random.randrange(2,13)
    print ("It landed on..")
    time.sleep(1)
    print(result)   
    try:
        answer= input("would you like to play again? [y/n]")
    except:
        pass
    if answer in ('y', 'Y'):
        return True
    return False

while rollDice1 or rollDice2():
    continue

1 个答案:

答案 0 :(得分:1)

def rollDice1():
    result = random.randrange(1,7)
    print ("It landed on..")
    time.sleep(1)
    print(result)

def rollDice2():
    result = random.randrange(2,13)
    print ("It landed on..")
    time.sleep(1)
    print(result)

def main():
    while True:
        numDice = input("Would you like one or two die?")
        if numDice ==  "1":
            rollDice1()
            break
        elif numDice == "2":
            rollDice2()
            break
        else:
            print("Invalid choice")
    while True:
        answer = input("would you like to play again? [y/n]").lower()
        if answer == "y":
            main()
        elif answer == "n":
           print ("Goodbye")
           break
        else:
            print("Invalid choice")
main()