2013-08-18 1 views
3

Я пытаюсь сделать простой маленький инструмент для преобразования дюймов в сантиметры и застрял при попытке ввести пользовательский ввод ('y' или 'n') для решая, нужно ли делать другое преобразование или прекратить действие. Вот что я сделал:Python: NameError name '[input]' не определен

import time 

def intro(): 
    print "Welcome! This program will convert inches to centimeters for you.\n" 
    convert() 

def convert(): 
    input_cm = input(("Inches: ")) 
    inches_conv = input_cm * 2.54 
    print "Centimeters: %f\n" % inches_conv 
    time.sleep(3) 
    restart = str(input("Do you wish to make another conversion? [y]Yes or [n]no: ")) 
    if restart == 'y': 
     convert() 
    elif restart == 'n': 
     end_fast() 
    else: 
     print "I didn't quite understand that answer. Terminating." 
     end_slow() 

def end_fast(): 
    print "This program will close in 5 seconds." 
    time.sleep(5) 

def end_slow(): 
    print "This program will close in 30 seconds." 
    time.sleep(30) 

intro() 

И это приводит:

Traceback (most recent call last): File "C:\Users\Sam\Programming\Python\the hard way\ex5.4.py", line 29, in intro() File "C:\Users\Sam\Programming\Python\the hard way\ex5.4.py", line 5, in intro convert() File "C:\Users\Sam\Programming\Python\the hard way\ex5.4.py", line 12, in convert restart = str(input("Do you wish to make another conversion? [y]Yes or [n]no?\n")) File "", line 1, in NameError: name 'y' is not defined

оцененная помощь.

+1

Использование '' не raw_input' input'. –

+0

@AshwiniChaudhary это ответ, кстати :) – alecxe

+0

Это действительно исправить это Ашвини. Большое спасибо! – ClutchHunter

ответ

6

вместо input попробовать raw_input:

заменить:

restart = str(input("Do you wish to make another conversion? [y]Yes or [n]no: ")) 

на:

restart = raw_input("Do you wish to make another conversion? [y]Yes or [n]no: ") 

 Смежные вопросы

  • Нет связанных вопросов^_^