我正在尝试编写一个在摄氏度和法伦海特之间进行转换的python函数,然后编写一个程序,该程序首先提示输入temp scale (c或f),然后提示输入temp值,然后再将其转换为其他值。到目前为止,我所拥有的:
def convert_temp( source_temp, scale):
if scale == 'c':
return(tmp-32.0)*(5.0/9.0)
elif scale == 'f':
return(tmp*(9.0/5/0))+32
source_temp = int(input)'Key in temp:'))
scale = input('(c) or (f)?')
y = conv(source_temp,scale)
print(tmp, 'in ',scale,"='s",y)但是,当我尝试运行该程序时,我收到了许多回溯和语法错误。我做错了什么??
发布于 2014-12-07 05:59:57
替换为:
9.0/5/0 # you will get ZeroDivisionError至:
9.0/5.0替换为:
source_temp = int(input)'Key in temp:')) # there should be opening bracket after input至:
source_temp = int(input('Key in temp:'))替换为:
y = conv(source_temp,scale)至:
y = conv_temp(source_temp,scale)更改您的print语句:
print(source_tmp, 'in ',scale,"='s",y) # here tmp was not defined, its source_tmp发布于 2014-12-07 06:10:15
你的代码中有很多问题。
def convert_temp( source_temp, scale):
if scale == 'c':
return(tmp-32.0)*(5.0/9.0)
elif scale == 'f':
return(tmp*(9.0/5/0))+32首先,tmp在此作用域中未定义。您的参数名为source_temp,而不是tmp。更改函数定义将修复该错误。此外,您还在一个表达式中犯了一个打字错误,并用斜杠替换了一个点。此函数将正常工作:
def convert_temp( tmp, scale):
if scale == 'c':
return(tmp-32.0)*(5.0/9.0)
elif scale == 'f':
return(tmp*(9.0/5.0))+32接下来,您在程序体中犯了一些语法错误:
source_temp = int(input)'Key in temp:'))此行中有一个不匹配的圆括号。它应该是
source_temp = int(input('Key in temp:'))再往下:
y = conv(source_temp,scale)conv()不是一个函数。相反,您应该使用定义的convert_temp()函数
y = convert_temp(source_temp,scale)最后,
print(tmp, 'in ',scale,"='s",y)现在未定义tmp。使用您定义的source_temp变量,如下所示:
print(source_temp, ' in ',scale," ='s ",y)发布于 2014-12-07 06:03:58
此语句中的不对称括号至少是问题的一部分:
source_temp = int(input)'Key in temp:'))试着这样做:
source_temp = int(input('Key in temp:'))还有:conv()不等同于convert_temp(),raw_input()不等同于input(),被零除等等。
https://stackoverflow.com/questions/27337035
复制相似问题