我又回到了我用简单的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 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:
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
答案 0 :(得分:1)
看起来像一个简单的拼写错误:
fahre = input(name + ", what is the temperature in Fahrenheit? "))
应删除最后)
。而你在上面的那一行中错过了一个。
答案 1 :(得分:0)
显然主要问题是第16行和第16行的操作数错误。 18.需要一个closing()和另一个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.")