Alberto Olivieri
Alberto Olivieri

Reputation: 25

Code runs in Python 3, not in Python 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!

Upvotes: 2

Views: 137

Answers (1)

hoefling
hoefling

Reputation: 66231

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

Upvotes: 2

Related Questions