Reputation: 1
I am BRAND new to python because my new school requires it. I am used to c++ so I'm still learning the ropes. I'm trying to make a dice rolling simulator and I thought i was doing everything right but my code just won't work. Any tips or guides to help me learn would be greatly appreciated. Here is my code:
import random
def roll(sides=6):
num_rolled = random.randint(l,sides)
return num_rolled
def main():
sides = 6
rolling = True
while rolling:
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!")
main()
This is the error I get:
Traceback (most recent call last): File "C:\Users\nomor\AppData\Local\Programs\Python\Python35-32\DiceRollingSim.py", line 20, in main() File "C:\Users\nomor\AppData\Local\Programs\Python\Python35-32\DiceRollingSim.py", line 13, in main num_rolled = roll(sides) File "C:\Users\nomor\AppData\Local\Programs\Python\Python35-32\DiceRollingSim.py", line 4, in roll num_rolled = random.randint(l,sides) NameError: name 'l' is not defined
Upvotes: 0
Views: 124
Reputation: 11635
For the first issue...
num_rolled = random.randint(l,sides)
l != 1
. You put an "l" instead of the number 1. Python thinks this is a variable which you haven't defined anywhere -> the error you get.
From the documentation randint takes two ints as parameters:
random.randint(a, b) Return a random integer N such that a <= N <= b. Alias for randrange(a, b+1).
Next, look at these lines in your code.
while rolling:
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
The if-else
part needs to be indented inside of the while loop:
while rolling:
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
Upvotes: 1