本文介绍了Python:未定义NameError名称'[input]'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to make a simple little tool for converting inches to centimeters and am stuck at trying to take a user input ('y' or 'n') for deciding whether to do another conversion or terminate. Here's what I've done:

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

结果是:

帮助表示赞赏.

推荐答案

而不是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: ")

这篇关于Python:未定义NameError名称'[input]'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 20:49
查看更多