不能';I don’我不能用python为这个程序编写脚本,有人能推荐一些东西让它工作吗
为了特定的目的,想知道不可变列表中的城市温度。他们有一定的标准来解决这个问题。标准如下所示: SL无要求规范 1提供一个选项,在该选项中,他们可以测量华氏或摄氏度的温度 2.输入要获取的温度值 3将它们放入一个列表或获取整个列表的输入 3将温度从摄氏度转换为华氏度,反之亦然,然后打印出来 华氏度到摄氏度的换算公式如下: T(°C)=(T(°F)-32)×5/9,此处T(°C)表示摄氏温度,T(°F)表示华氏温度。 摄氏度到华氏度的换算公式如下: T(°F)=T(°C)×9/5+32,此处T(°C)表示摄氏温度,T(°F)表示华氏温度。 请为此编写一个python脚本。请至少包含一个用户定义的函数。可以使用tuple()函数将列表转换为tuple 我的剧本如下不能';I don’我不能用python为这个程序编写脚本,有人能推荐一些东西让它工作吗,python,Python,为了特定的目的,想知道不可变列表中的城市温度。他们有一定的标准来解决这个问题。标准如下所示: SL无要求规范 1提供一个选项,在该选项中,他们可以测量华氏或摄氏度的温度 2.输入要获取的温度值 3将它们放入一个列表或获取整个列表的输入 3将温度从摄氏度转换为华氏度,反之亦然,然后打印出来 华氏度到摄氏度的换算公式如下: T(°C)=(T(°F)-32)×5/9,此处T(°C)表示摄氏温度,T(°F)表示华氏温度。 摄氏度到华氏度的换算公式如下: T(°F)=T(°C)×9/5+32,此处T(°C
n=int(input("Please enter the temperature observation"))
emptyList1=float[]
emptyList2 = float[]
C = float
F = float
for i in range (0,n):
t=(i+1 ," th Value")
emptyList1.append(t)
def C ():
C = (t-32)*(5/9)
def F ():
F = (t*(9/5)) + 32
print( "Enter C if the given temperature is in Fahrenheit and want to convert in Celcius, F if the temperature is in Celcius and want to convert in Farhenheit")
choice = input('Please enter your selected option: ')
if (choice==C):
C()
elif (choice==F):
F()
for t in range (emptyList1):
t1 = C(t)
emptyList2.append(t)
这有许多语法问题 首先,不要将变量和方法命名为相同的名称 其次,将类型分配给变量(例如
C=float
)没有意义
第三,首先不应该使用C
或F
变量,只需从方法中返回一个值即可
最后,if(choice==C)
应该是if(choice==C')
,因为您希望将输入与字符串'C'
进行比较,而不是与名为C
的变量进行比较
print("Welcome to temperature converter.\n\n")
unit=input("Press 'f' for Fahrenheit or 'c' for celsius:")
list_1=[]
answer_list=[]
if unit.lower()=="f":
try:
number=int(input("\nPlease type the number of times you want to convert: "))
for i in range(1,number+1):
x=float(input(f"Input - {i} Enter your temperature in Fahrenheit: "))
list_1.append(x)
for times in list_1:
answer=(times-32)*5/9
answer_list.append(answer)
print("\nHere are your answers: \n")
for k in range(1,number+1):
print(f"Answer - {k} Your answer is: "+str(answer_list[k-1])+"°C")
except Exception as e:
print(e)
print("Invalid Number Provided.")
elif unit.lower()=="c":
try:
number=int(input("\nPlease type the number of times you want to convert: "))
for i in range(1,number+1):
x=float(input(f"Input - {i} Enter your temperature in Celsius: "))
list_1.append(x)
for times in list_1:
answer=(times*9/5)+32
answer_list.append(answer)
print("\nHere are your answers: \n")
for k in range(1,number+1):
print(f"Answer - {k} Your answer is: "+str(answer_list[k-1])+"°F")
answer_list.clear()
list_1.clear()
except Exception as e:
print(e)
print("Invalid Number Provided.")
else:
print("Invalid input Provided.")
C=float
是错误的-您不能将类型分配给这样的变量。我唯一的建议是实际执行一些python教程。您的代码显然不了解基本的python原则(如函数中的return和创建列表),而且,if(choice==C)
是错误的-我假设您的意思是if(choice==C')
。您希望将其与字符串进行比较,而不是与变量C
进行比较。另外,我建议你不要让你的变量和方法像那样有相同的名字。。。。我一直在想我做错了什么,但是我想不出来。我对python和语言基本上是非常陌生的。这才几个星期,所以是的,我的脚本中有很多错误,我正在努力学习,有人说我应该去Youtube教程,但是Youtube不会告诉我哪里错了,这就是我来这里的原因,我不是在寻找解决方案,只是想得到一些建议,使我的脚本正确。谢谢你的帮助我真的很感激汉克斯·苏杰我会试试这个。我从来没有使用过这个“try:”之前,你能解释一下它是什么以及它是如何工作的吗?@mary Stack Overflow不是一个教程资源,我建议阅读。我没有投反对票,但最好包括更多的解释,以便OP可以学习。try and Exception方法: