我正在尝试制作一个简单的小工具,用于将英寸转换为厘米,并且一直试图获取用户输入(“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 he Hard way x5.4.py”,第 29 行,位于 介绍() 文件“C:\Users\Sam\Programming\Python he Hard way x5.4.py”,第 5 行,简介 转变() 文件“C:\Users\Sam\Programming\Python he Hard way x5.4.py”,第 12 行,在转换中 restart = str(input("您希望进行另一次转换吗?[y]是还是[n]否? ”)) 文件“”,第 1 行,位于 名称错误:名称“y”未定义
感谢帮助。
而不是
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: ")