I am pretty new to Python and I am just doing a bunch of exercises.
This is one of them, a simple DiceRoller.
It works perfectly fine in ATOM, the issue arises when I try to run it in IDLE. I am unable to figure out why the issue happen. Pretty sure this is a noob question.
The code:
import random
dices=[2, 3, 4, 6, 8, 10, 12, 20, 100]
Y= ['yes', 'y']
N= ['no', 'n']
def DiceRoller():
dice_selection=input('Please, choose the dice(d2, d3, etc. - only the number): ')
try:
dice = int(dice_selection)
except ValueError:
print('You have to select a number, try again')
DiceRoller()
if dice not in dices:
print('You have to select a 2, 3, 4, 6, 8, 10, 12, 20, 100 faces dice, try again')
DiceRoller()
number=input('How many dice(s) do you want to roll? ')
try:
numint = int(number)
except ValueError:
print('You have to select a number, try again')
DiceRoller()
ripet=0
while ripet < numint:
ripet += 1
if dice in dices:
result=random.randint(1,dice)
print(result)
else:
Continue()
def Continue():
risposta=input('Do you want to roll again? (Y/N) ')
rispostal= risposta.lower()
if rispostal in Y:
DiceRoller()
elif rispostal in N:
return 'Goodbye'
quit()
else:
print('Please, answer Yes or No')
Continue()
DiceRoller()
Errors whit IDLE after the program ask me if I want to roll again (input y or n):
Traceback (most recent call last):
File "E:\Corso Python\DiceRoller.py", line 44, in <module>
DiceRoller()
File "E:\Corso Python\DiceRoller.py", line 30, in DiceRoller
Continue()
File "E:\Corso Python\DiceRoller.py", line 33, in Continue
risposta=input('Do you want to roll again? (Y/N) ')
File "<string>", line 1, in <module>
NameError: name 'y' is not defined
Errors whit IDLE after the program ask me if I want to roll again (input Y or N):
Traceback (most recent call last):
File "E:\Corso Python\DiceRoller.py", line 44, in <module>
DiceRoller()
File "E:\Corso Python\DiceRoller.py", line 30, in DiceRoller
Continue()
File "E:\Corso Python\DiceRoller.py", line 34, in Continue
rispostal= risposta.lower()
AttributeError: 'list' object has no attribute 'lower'
Thank you for your patience!
That's because in the atom editor, you use python3 and your IDLE uses python2. In Python 2, the function for reading user's input was called raw_input(); it was renamed to input() in Python 3 (section starting with PEP 3111: raw_input() was renamed to input()).
You can either ensure you are using python3 by default or make the code python2-compatible: add a code block
import sys
compatible_input = raw_input if sys.version_info < (3, 0) else input
and replace all usages of ... = input(...) in your code with ... = compatible_input(...).
Related
during tests it's showing the error message:
Traceback (most recent call last):
File "tictactoe/tictactoe.py", line 10, in <module>
coordinates = input("Enter the coordinates: ")
EOFError: EOF when reading a line
I can't image what is causing this problem. The code is:
c = True
while c:
coordinates = input("Enter the coordinates: ") # <-- line 10 is this one
if coordinates == '':
print("Coordinates should be from 1 to 3!")
continue
elif len(coordinates) < 3 or len(coordinates) > 3:
print("Coordinates should be from 1 to 3!")
continue
Thanks for support
Your test probably runs the code non-interactively. If you are creating the test in Python, try using subprocess.PIPE so that you can communicate with the subprocess. Without a valid stdin, the program will throw this error, which (should) cause your test to fail.
This the code that has the error:
print("Through 1-10 write a number that is going to represent how far you should throw the ball for " + playerCMD4 + "to catch") ; sleep(float(speed))
playerNumberCMD = raw_input()
import random
def allResponses(arg1):
allResponses = arg1
allResponses = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
def randomResponse(arg1):
randomResponse = arg1
randomResponse = random.choice(allResponses)
if randomResponce == playerNumberCMD:
print(playerCMD4 + " caught the ball.") ; sleep(float(speed))
The error I get is this:
Traceback (most recent call last):
File "classref3.py", line 3, in <module>
class Dog:
File "classref3.py", line 50, in Dog
if randomResponce == playerNumberCMD:
NameError: name 'randomResponce' is not defined
Was Defnot the correct way to go or is it something else?
As the error states you have not defined the variable "randomResponce".
If you look at the previous line where you think you have defined the variable, you have defined "randomResponse". Note the different spelling. The spelling needs to be the same.
I would also caution against using the same name for a variable and a function in the same script.
I wanted to write a function that prints True if the pin the user inputted was 4 or 6 in length. I wrote the function, but when I run it outputs this error. How do I fix this error and what is wrong with it in the first place?
pin = raw_input("Please enter your four or six digit pins")
def validate_pin(pin):
if len(pen) == 4 or 6:
purp = True
else:
purp = False
return(purp)
print purp
validate_pin(pin)
Traceback (most recent call last):
File "main.py", line 1, in <module>
from solution import *
File "/home/codewarrior/solution.py", line 1, in <module>
pin = input("Please enter your four or six digit pins")
EOFError: EOF when reading a line
There are several errors in your code, however, I was unable to create your specific error message. Most likely your raw_input failed.
Here is your fixed code:
pin = raw_input("Please enter your four or six digit pins")
def validate_pin(pin):
if len(pin) in [4, 6]:
# your old code results in two boolean statements
# 1) len(pin) == 4, this correctly checks for a length of 4
# 2) or 6, this is always true, result in your if statement
# always being true
purp = True
else:
purp = False
print purp
# if your print statement comes after a return statement,
# it is never executed
return(purp)
validate_pin(pin)
import sys
def main():
keystr = input("Enter the Key: ")
key = int(keystr)
if (key <=0) or (key>=25):
print("The key is out of range")
sys.exit()
When I want to terminate with input of key (<= 0 or >= 25), there is an error messgae.
The key is out of rangeTraceback (most recent call last):
File "C:/Users/USER/Desktop/caesarRefactored.py", line 38, in <module>
main()
File "C:/Users/USER/Desktop/caesarRefactored.py", line 11, in <module>
sys.exit()
builtins.SystemExit:
How can I fix it?
I'm running this on wing idle, the error shows. If on the terminal, it works just fine.
Trying to write a function that will iterate over the linked list, sum up all of the odd numbers and then display the sum. Here is what I have so far:
from List import *
def main():
array = eval(input("Give me an array of numbers: "))
ArrayToList(array)
print(array[0])
print(array[1])
print(array[2])
print(array[3])
print(sumOdds(array))
def isOdd(x):
return x % 2 != 0
def sumOdds(array):
if (array == None):
return 0
elif (isOdd(head(array))):
return head(array) + sumOdds(tail(array))
else:
return sumOdds(tail(array))
main()
I can't get it to actually print the sum though. Can anybody help me out with that?
Here is the output of the program when I run it:
$ python3 1.py
Give me an array of numbers: [11, 5, 3, 51]
Traceback (most recent call last):
File "1.py", line 22, in <module>
main()
File "1.py", line 10, in main
print(sumOdds(array))
File "1.py", line 19, in sumOdds
return head(array) + sumOdds(tail(array))
File "1.py", line 18, in sumOdds
elif (isOdd(head(array))):
File "/Users/~/cs150/practice3/friday/List.py", line 34, in head
return NodeValue(items)
File "/Users/~/cs150/practice3/friday/List.py", line 12, in NodeValue
def NodeValue(n): return n[0]
TypeError: 'int' object is not subscriptable
In Python you iterate through a list like this:
list_of_numbers = [1,4,3,7,5,8,3,7,24,23,76]
sum_of_odds = 0
for number in list_of_numbers:
# now you check for odds
if isOdd(number):
sum_of_odds = sum_of_odds + number
print(sum_of_odds)
List is also a module only on your computer. I do not know what is inside. Therefore, I can not help you after ArrayToList(array).