Python:NameError名称'[input]'未定义

时间:2013-08-18 11:50:09

标签: python undefined nameerror

我正在尝试制作一个简单的小工具,用于将英寸转换为厘米,并且在尝试接受用户输入('y'或'n')时决定是否进行另一次转换或终止。这就是我所做的:

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

这导致:

  

追踪(最近一次通话):     文件“C:\ Users \ Sam \ Programming \ Python \ the hard way \ ex5.4.py”,第29行,中       介绍()     文件“C:\ Users \ Sam \ Programming \ Python \ the hard way \ ex5.4.py”,第5行,in intro       兑换()     文件“C:\ Users \ Sam \ Programming \ Python \ the hard way \ ex5.4.py”,第12行,在转换中       restart = str(输入(“你想进行另一次转换吗?[y]是或[n]否?\ n”))     文件“”,第1行,in   NameError:名称'y'未定义

帮助表示赞赏。

1 个答案:

答案 0 :(得分: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: ")