2

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!

3
  • 2
    I bet that IDLE runs python2.7 and that's why input makes an error... Use raw_input in python2.7 and input in python3 Commented Nov 24, 2017 at 14:01
  • docs.python.org/2/library/functions.html#input In python2 string returned from input() is immediately evaluated which causes code to behave like risposta=y where y is not defined yet in first case, and risposta=Y where Y is an array in second. Commented Nov 24, 2017 at 14:03
  • It works for me in IDLE with Python 3.6.2 Commented Nov 24, 2017 at 14:04

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

To get the code to work with an IDLE using python2, I added input=raw_input at the top. Not the ideal solution, but it proves the case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.