Python 另一个缺少语法的帖子
对,所以我在为火车到达某个目的地所需的燃油量编写代码,这些是代码的限制/必要性: 询问用户行程将达到多少公里 仅当用户输入大于零的值时才继续 将燃油量设置为公里数的100倍 不允许燃油量小于1500个单位 显示所需的燃油量Python 另一个缺少语法的帖子,python,Python,对,所以我在为火车到达某个目的地所需的燃油量编写代码,这些是代码的限制/必要性: 询问用户行程将达到多少公里 仅当用户输入大于零的值时才继续 将燃油量设置为公里数的100倍 不允许燃油量小于1500个单位 显示所需的燃油量 这就是我到目前为止所得到的,它指出有语法错误,但我找不到它们,因为我对一般的编码相当陌生。 我不知道为什么它没有全部放进盒子里 Km = 0 Fuel = 0 Extra = 0 print ("How long is the journey in Km?") Km
这就是我到目前为止所得到的,它指出有语法错误,但我找不到它们,因为我对一般的编码相当陌生。 我不知道为什么它没有全部放进盒子里
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")
Km=0
燃料=0
额外=0
打印(“以公里为单位的旅程有多长?”)
Km=输入(“Km数”)
如果Km==0:
燃油=公里*100
如果燃油==0<1500:
额外里程=1500公里
燃料==燃料+额外燃料
打印(“添加了额外的“+额外的”燃料单位”)
其他的
其他的
打印(“请输入有效数字”)
打印(“您需要“+燃料”单位的燃料才能到达目的地”)
Km=0
燃料=0
额外=0
打印(“以公里为单位的旅程有多长?”)
Km=输入(“Km数”)
如果Km==0:
燃油=公里*100
如果燃油==0<1500:;==比照
额外里程=1500公里;=分配变量
燃料=燃料+额外燃料
打印(“添加了额外的“+额外的+”燃料单位”);+不见了
其他的
其他的
打印(“请输入有效数字”)
打印(“您需要“+燃油+”单位的燃油才能到达目的地”);+不见了
您的代码不是很好,因为您没有使用编码指南,但这对初学者来说不是问题,但ok,这是正确的代码。如果你不明白,可以问我:
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")
Km=0
燃料=0
额外=0
打印(“以公里为单位的旅程有多长?”)
Km=输入(“Km数”)
如果公里!=0:
燃油=公里*100
如果燃油<1500且燃油>0:
额外里程=1500公里
燃油+=额外燃油
打印(“添加了额外的“+额外的+”燃料单位”)
其他的
打印(“请输入有效数字”)
打印(“您需要“+燃油+”单位的燃油才能到达目的地”)
我不确定我是否真的理解了你的代码,所以就问我吧 有不止几个语法错误,还有一些逻辑错误。我留下了一些评论来解释我发现并修复的一些错误
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
=
用于比较,=
用于设置变量打印(“一个额外的”+extra”单位
:额外单位和单位之间缺少一个+
符号来对字符串求和。“它表示存在语法错误”。“它是这样写的。”“没有说明它们在哪一行?Python通常会这样做。你不能同时将Fuel与0和1500进行比较。决定你指的是哪一行。@stark我想他是指两者:Fuel>0和Fuel<1500
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