python input nameerror_Python:NameError名称'[input]'未定义

我正在尝试制作一个简单的小工具来将英寸转换为厘米,并且一直致力于获取用户输入(“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()

结果是: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

感谢帮助。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值