2017-11-24 1 views
2

저는 Python을 처음 접했고 많은 연습을하고 있습니다. 이것들은 간단한 DiceRoller 중 하나입니다. ATOM에서 완벽하게 작동하지만 IDLE에서 실행하려고하면 문제가 발생합니다. 문제가 발생하는 이유를 파악할 수 없습니다. 확실히 이것은 멍청한 질문입니다. 코드 : 프로그램 후코드는 Python 2가 아닌 Python 3에서 실행됩니다.

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

오류 오순절 IDLE 내가 다시 (입력 Y 또는 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 

오류 오순절 IDLE을 다시 (입력 Y 또는 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' 

가 기다려 주셔서 감사합니다!

+2

나는 idle이 python2.7을 돌리고 그 때문에'input'이 오류를 일으킬 것입니다 ... python2.7에서'raw_input'을 사용하고 python3에서'input'을 사용합니다 – Vyko

+0

https://docs.python.org/2/ library/functions.html # input python2에서'input()'에서 반환 된 문자열은 즉시 평가됩니다.이 경우'risposta = y'와 같이 행동하게되고'y '는 처음에는 정의되지 않고'risposta = Y' 여기서'Y'는 두 번째 배열입니다. –

+0

IDLE with Python 3.6.2 – quamrana

답변

2

원자 편집기에서 python3을 사용하고 IDLE에서 python2을 사용하기 때문입니다. 파이썬 2에서 사용자 입력을 읽는 함수는 raw_input()이라고 불렀다. it was renamed to input() in Python 3 (섹션 시작은 PEP 3111: raw_input() was renamed to input()).

당신은 당신이 기본적으로 python3을 사용하고 있는지 확인하거나 코드가 python2 호환 할 수 있습니다 : 코드 블록

import sys 
compatible_input = raw_input if sys.version_info < (3, 0) else input 

를 추가하고 ... = compatible_input(...)와 코드에서 ... = input(...)의 모든 용도를 교체하십시오.

+0

python2를 사용하여 IDLE로 작업 할 코드를 얻으려면 맨 위에'input = raw_input'을 추가했습니다. 이상적인 해결책은 아니지만 그 사실을 증명합니다. – quamrana

관련 문제