是的,所以我正在为火车到达某个目的地所需的燃料量编写代码,这些是代码的限制/必要性:
询问用户旅程的公里数 仅在用户输入大于零的值时才继续 将燃油量设定为公里数的100倍 不允许燃料量小于1500单位 显示所需的燃料量
这就是我到目前为止所说的,它表示存在语法错误,但我无法找到它们,因为我对编码很新。 我不知道为什么它没有全部进入框中
Km = 0
Fuel = 0
Extra = 0
print ("How long is the journey in Km?")
Km = input("Number of Km")
if Km == 0:
Fuel = Kilometers*100
if Fuel == 0 < 1500:
Extra == 1500-Kilometers
Fuel == Fuel + Extra
print ("An extra" +Extra "units of fuel were added")
else
else
print ("Please enter a valid number")
print ("You need" +Fuel "units of fuel to reach your destination")
答案 0 :(得分:1)
Km = 0
Fuel = 0
Extra = 0
print ("How long is the journey in Km?")
Km = input("Number of Km")
if Km == 0:
Fuel = Kilometers*100
if Fuel == 0 < 1500: ; == to compare
Extra = 1500-Kilometers ; = to assign varable
Fuel = Fuel + Extra
print ("An extra" +Extra+ "units of fuel were added") ; + was missing
else
else
print ("Please enter a valid number")
print ("You need" +Fuel+ "units of fuel to reach your destination") ; + was missing
答案 1 :(得分:0)
您的代码并不是很好,因为您没有使用编码指南,但这对初学者来说不是问题,但这里的代码是正确的。如果你不理解某些事情,你可以问我:
Km = 0
Fuel = 0
Extra = 0
print ("How long is the journey in Km?")
Km = input("Number of Km")
if Km != 0:
Fuel = Kilometers*100
if Fuel < 1500 and Fuel > 0:
Extra = 1500-Kilometers
Fuel += Extra
print ("An extra of " + Extra + " units of fuel were added")
else
print ("Please enter a valid number")
print ("You need" + Fuel + "units of fuel to reach your destination")
我不确定我是否真的理解你的代码所以请问我!
答案 2 :(得分:0)
有多个语法错误,还有一些逻辑错误。我留下了一些评论来解释我发现和修复的一些错误。
Km = 0
Fuel = 0
Extra = 0
print ("How long is the journey in Km?")
Km = int(input("Number of Km: ")) # Convert to integer
if Km >= 0: # You want to check if it is not negative
Fuel = Km*100 # You used a new variable called 'Kilometers' and not Km you had set above
if Fuel > 1500:
Extra = Fuel - 1500 # Bad math fixed?
Fuel = Fuel + Extra
print ("An extra " +str(Extra) +" units of fuel were added") # Missing + and str cast on variable
else: # Missing colon
Fuel = 1500 # Missing statement?
else:
print ("Please enter a valid number")
print ("You need " +str(Fuel)+ " units of fuel to reach your destination") # Missing + and str cast on variable