我正在嘗試製作一個簡單的小工具,用於將英寸轉換爲釐米,並堅持嘗試將用戶輸入('y'或'n')決定是否做另一次轉換或終止。下面是我做了什麼:Python:NameError name'[input]'未定義
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
幫助表示讚賞。
使用'raw_input'不'input'。 –
@AshwiniChaudhary這是一個答案,順便說一句:) – alecxe
這確實確定了它Ashwini。非常感謝! – ClutchHunter