Python:未定义NameError名称'[input]' [英] Python: NameError name '[input]' is not defined

查看:98
本文介绍了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()

结果是:

回溯(最近通话最近): 文件"C:\ Users \ Sam \ Programming \ Python \ the hard way \ ex5.4.py",第29行,在 介绍() 文件"C:\ Users \ Sam \ Programming \ Python \ hard way \ ex5.4.py",第5行,简介 转变() 转换后的文件"C:\ Users \ Sam \ Programming \ Python \ the hard way \ ex5.4.py",第12行 restart = str(input(您是否要进行另一次转换?[y]是或[n]否?\ n")) 文件",第1行,在 NameError:名称"y"未定义

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

帮助表示赞赏.

推荐答案

而不是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]'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆