Search code examples
pythondice

Python Dice Game error


[Solved] It was a stupid typo. Sorry.

I'm currently learning python and I'm running into some errors, if you could point out how I could fix them and why they are errors that would be great as I learn through trial and error with projects like these. The error;

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'

My code;

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

Solution

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