gpt4 book ai didi

Python: NameError 名称 '[input]' 未定义

转载 作者:太空宇宙 更新时间:2023-11-03 13:46:52 26 4
gpt4 key购买 nike

<分区>

我正在尝试制作一个简单的小工具来将英寸转换为厘米,但一直坚持尝试接受用户输入(“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

感谢帮助。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com