Python 为什么两者都不能;如果;速度分支<;70 nor速度>;70火?
这是我的问题 系统为您提供了两个输入值,用于确定两辆车的速度Python 为什么两者都不能;如果;速度分支<;70 nor速度>;70火?,python,Python,这是我的问题 系统为您提供了两个输入值,用于确定两辆车的速度 如果两辆车的行驶速度都超过70,则输出“2辆快车” 如果只有一辆车以超过70的速度行驶,则输出“1辆快车” 否则输出“无快车” 这是我的密码 import sys speed1 = int(sys.argv[1]) speed2 = int(sys.argv[2]) if speed1 > 70 and speed2 > 70: print ('2 fast cars') if speed1 > 70 an
如果两辆车的行驶速度都超过70,则输出“2辆快车”
如果只有一辆车以超过70的速度行驶,则输出“1辆快车”
否则输出“无快车”
这是我的密码
import sys
speed1 = int(sys.argv[1])
speed2 = int(sys.argv[2])
if speed1 > 70 and speed2 > 70:
print ('2 fast cars')
if speed1 > 70 and speed2 < 70:
print ('1 fast car')
if speed1 < 70 and speed2 > 70:
print ('1 fast cars')
if speed1 < 70 and speed2 < 70:
print ('no fast cars')
我确信这只是一个语法错误,但我找不到它,谢谢。在我看来,您的程序似乎不处理速度为70的情况,它只检查>70,并且错误是非常描述性的:您忘记考虑一个情况:当一辆车(或两辆车)以70(公里/小时或英里/小时)的速度行驶时。在这种情况下,所有
if
语句都将失败,因此不会写入任何输出
一个快速修复方法是重写
您没有任何语法错误。在您提供的脚本中,您已经说过,如果速度更高(
)或更低(),那么有什么不清楚的错误?您可以与70
进行比较,但如果其中一个(或两个)发生错误,则不会触发任何案例正是70。如果我的代码正在运行,我不确定这是否只是语法错误。顺便说一句,一般来说,如果你试图在特定的地方建立一个隔离区,将代码与你期望的不一致的地方隔离开来,这将有助于提供更好的接收效果。因此,你可能只有两行代码:一行是分配speed1=70
,另一个正在运行,打印(speed1<70)
,然后让问题询问为什么输出为False
,如果您希望输出为True
。(如果你没有充分考虑你的代码,没有识别出你期望的具体情况是True
,而不是False
,那么你还没有准备好在这里提问)。谢谢这一修正。我的作业没有真正弄清楚elif。
import sys
speed1 = int(sys.argv[1])
speed2 = int(sys.argv[2])
if speed1 > 70 and speed2 > 70:
print ('2 fast cars')
if speed1 > 70 and speed2 <= 70:
print ('1 fast car')
if speed1 <= and speed2 > 70:
print ('1 fast cars') # probably a typo? cars -> car
if speed1 <= 70 and speed2 <= 70:
print ('no fast cars')
import sys
speed1 = int(sys.argv[1])
speed2 = int(sys.argv[2])
if speed1 > 70 and speed2 > 70:
print ('2 fast cars')
elif speed1 > 70 or speed2 > 70:
print ('1 fast car')
else:
print ('no fast cars')
if speed1 == 70 and speed2 == 70:
print ('2 fast cars both at 70')
if speed1 <= 70 and speed2 <= 70:
print ('2 fast cars either both at 70 or under 70')
if speed1 >= 70 and speed2 >= 70:
print ('2 fast cars either both at 70 or over 70')