首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NameError :未定义NameError名称“[输入]”

NameError :未定义NameError名称“[输入]”
EN

Stack Overflow用户
提问于 2013-08-18 11:50:09
回答 1查看 10.1K关注 0票数 3

我试图制作一个简单的小工具,将英寸转换为厘米,并试图使用用户输入('y‘或'n')来决定是进行另一次转换还是终止。以下是我所做的:

代码语言:javascript
复制
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\ hard way\ex5.4.py",第29行,intro() File "C:\Users\Sam\Programming\Python\the hard way\ex5.4.py",第5行,在intro () File”C:\Users\Sam\Programming\the hard way\ex5.4.py“中,第12行,在转换重新启动=str(输入(”您希望进行另一次转换吗?yYes或nno?\n)文件"",第1行,在NameError中:未定义名称'y‘

帮助感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-08-18 11:55:04

而不是input,尝试raw_input

取代:

代码语言:javascript
复制
restart = str(input("Do you wish to make another conversion? [y]Yes or [n]no: "))

在以下位置:

代码语言:javascript
复制
restart = raw_input("Do you wish to make another conversion? [y]Yes or [n]no: ")
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18298741

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档