Python 3度量转换项目语法错误
我的简单Python项目又遇到了一个更大的问题。显然不仅仅是16号线。还有别的事,我想不出来。非常感谢你的智慧。项目代码如下:Python 3度量转换项目语法错误,python,data-conversion,Python,Data Conversion,我的简单Python项目又遇到了一个更大的问题。显然不仅仅是16号线。还有别的事,我想不出来。非常感谢你的智慧。项目代码如下: #Request user's name name = input("Hello! I'm your friendly metric conversion robot. What is your first name? ") #Miles to Km Conversion #Request miles & format for flo
#Request user's name
name = input("Hello! I'm your friendly metric conversion robot. What is your first name? ")
#Miles to Km Conversion
#Request miles & format for float
miles = float(input(name + ", how many miles do you want to convert to kilometers? "))
#Convert miles to kilometers
milesToKm = float(miles * 1.6)
#Display the result
print(name + ", there are " + str(format(milesToKm,'.2f') + " in " + str(miles) + " miles, ")
#Fahrenheit to Celsius Conversion
#Request Fahrenheit temperature
fahre = input(name + ", what is the temperature in Fahrenheit? "))
#Convert Fahrenheit to Celsius
fToC = float((fahre - 32)*(5/9))
#Display result
print(name + ", there are "+ str(format(fToC,'.2f')) + " Celsius in " + str(fahre) + " Fahrenheit degrees.")
#Gallons to Liters Conversion
#Request gallons
gallons = float(input(name + ", how many gallons do you want to convert to liters?"))
#Convert gallons to liters
galToLiters = float(gallons * 3.9)
#Display results
print(name + ", there are " + str(format(galToLiters,'.2f')) + " liters in " + str(gallons) + " gallons.")
#Pounds to Kilograms Conversion
#Request pounds
lbs = float(input(name + ", how many pounds do you want to convert to kilograms?"))
#Convert pounds to kilograms
lbsToKilos = float(lbs *0.45)
#Display results
print(name + ", there are " + str(format(lbsToKilos,'.2f')) + " in " + str(lbs) + " U.S. pounds.")
#Inches to Centimeters Conversion
#Request inches
inches = float(input(name + ", how many inches do you want to convert to centimeters? "))
#Convert inches to centimeters
inchesToCm = float(inches *2.54)
#Display results
print(name + ", there are " + str(format(inchesToCm,'.2f')) + " in " + str(inches) + " inches.")
对于语法错误,我得到了相同的回溯:
Traceback (most recent call last):
File "/Applications/Komodo IDE 8.app/Contents/SharedSupport/dbgp/bin/py3_dbgp", line 310, in <module>
sys.exit( main(sys.argv) )
File "/Applications/Komodo IDE 8.app/Contents/SharedSupport/dbgp/bin/py3_dbgp", line 284, in main
dbgp.client.runWithoutDebug(args, interactive, host, port, idekey, logLevel)
File "/Applications/Komodo IDE 8.app/Contents/SharedSupport/dbgp/python3lib/dbgp/client.py", line 4016, in runWithoutDebug
h_execfile(debug_args[0], debug_args, module=main)
File "/Applications/Komodo IDE 8.app/Contents/SharedSupport/dbgp/python3lib/dbgp/client.py", line 675, in __init__
exec(contents, globals, locals)
File "<string>", line 16
fahre = input(name + ", what is the temperature in Fahrenheit? ")
^
SyntaxError: invalid syntax
回溯(最近一次呼叫最后一次):
文件“/Applications/Komodo IDE 8.app/Contents/SharedSupport/dbgp/bin/py3_dbgp”,第310行,在
系统出口(主(系统argv))
文件“/Applications/Komodo IDE 8.app/Contents/SharedSupport/dbgp/bin/py3_dbgp”,主文件第284行
runWithoutDebug(args、interactive、host、port、idekey、logLevel)
文件“/Applications/Komodo IDE 8.app/Contents/SharedSupport/dbgp/python3lib/dbgp/client.py”,第4016行,运行时不带调试
h_execfile(debug_args[0],debug_args,module=main)
文件“/Applications/Komodo IDE 8.app/Contents/SharedSupport/dbgp/python3lib/dbgp/client.py”,第675行,在__
执行官(目录、全局、局部)
文件“”,第16行
fahre=输入(名称+”,以华氏度为单位的温度是多少?”)
^
SyntaxError:无效语法
看起来像一个简单的打字错误:
fahre = input(name + ", what is the temperature in Fahrenheit? "))
应该删除最后一个
)
。上面一行缺少一个。显然,主要问题是第16行和第18行的操作数错误。需要一个结束()和另一个float()声明。下面是正确的代码。谢谢大家的帮助!:)
看起来该行上方的行缺少括号。
#Request user's name
name = input("Hello! I'm your friendly metric conversion robot. What is your first name? ")
#Miles to Km Conversion
#Request miles & format for float
miles = float(input(name + ", how many miles do you want to convert to kilometers? "))
#Convert miles to kilometers
milesToKm = float(miles * 1.6)
#Display the result
print(name + ", there are " + str(format(milesToKm,'.2f')) + " in " + str(miles) + " miles, ")
#Fahrenheit to Celsius Conversion
#Request Fahrenheit temperature
fahre = float(input(name + ", what is the temperature in Fahrenheit? "))
#Convert Fahrenheit to Celsius
fToC = float((fahre - 32)*(5/9))
#Display result
print(name + ", there are "+ format(fToC,'.2f') + " Celsius in " + str(fahre) + " Fahrenheit degrees.")
#Gallons to Liters Conversion
#Request gallons
gallons = float(input(name + ", how many gallons do you want to convert to liters?"))
#Convert gallons to liters
galToLiters = float(gallons * 3.9)
#Display results
print(name + ", there are " + str(format(galToLiters,'.2f')) + " liters in " + str(gallons) + " gallons.")
#Pounds to Kilograms Conversion
#Request pounds
lbs = float(input(name + ", how many pounds do you want to convert to kilograms?"))
#Convert pounds to kilograms
lbsToKilos = float(lbs *0.45)
#Display results
print(name + ", there are " + str(format(lbsToKilos,'.2f')) + " in " + str(lbs) + " U.S. pounds.")
#Inches to Centimeters Conversion
#Request inches
inches = float(input(name + ", how many inches do you want to convert to centimeters? "))
#Convert inches to centimeters
inchesToCm = float(inches *2.54)
#Display results
print(name + ", there are " + str(format(inchesToCm,'.2f')) + " in " + str(inches) + " inches.")