Reputation: 609
I was trying to make a program that generate a number within range of 1-6 randomly. And will stop when the user input 'N'
Here is my code
import random
def roll():
min = 1
max = 6
value = random.randrange(min,max+1)
print(value)
x = 'Y'
while (x == 'Y'):
roll()
x = chr(input("again?(Y/N)"))
and this is the error message
Traceback (most recent call last): File "Dice.py", line 11, in x = chr(input("again?(Y/N)")) File "", line 1, in NameError: name 'Y' is not defined
Upvotes: 0
Views: 89
Reputation: 4526
change your code to use str
instead of chr
chr() : Return a string of one character whose ASCII code is the integer i. For example, chr(97) returns the string 'a'. The argument must be in the range [0..255]
import random
def roll():
min = 1
max = 6
value = random.randrange(min,max+1)
print(value)
x = 'Y'
while (x == 'Y'):
roll()
x = input("again?(Y/N)") #Python 3
x = raw_input("again?(Y/N)") #Python 2
Upvotes: 0
Reputation:
Use raw_input in Python 2 to get a string, input in Python 2 is equivalent to eval(raw_input). So, When you enter something like Y in input it thinks that you're looking for a variable named Y:
import random
def roll():
min = 1
max = 6
value = random.randrange(min,max+1)
print(value)
x = 'Y'
while (x == 'Y'):
roll()
x = (raw_input("again?(Y/N)"))
USE raw_input()
and remove chr()
function
Upvotes: 1
Reputation: 609
this works thank you
import random
def roll():
min = 1
max = 6
value = random.randrange(min,max+1)
print(value)
x = 'Y'
while (x == 'Y'):
roll()
x = raw_input("again?(Y/N)")
Upvotes: 0