如何在Python中修复比较测试?



python和stackoverflow新手。如果我的问题格式不正确,请向我道歉。

我正在尝试模拟一个保护鱼类的应用程序。如果比目鱼的长度超过12英寸,用户应该得到一条信息,将其扔回;小于或等于12英寸的可以保留。

我知道我的错误是<=,但我不知道如何修复它。

Github链接到全部代码。

谢谢。

if fishLength ['Winter Flounder'] <= maxFishLength['Winter Flounder']:

#What is the maximum length of the fish?
#Use the dictionary module {}
maxFishLength = {
"Acadian Redfish" : math.inf,
"Winter Flounder" : 12
}
fishTally = {
"Acadian Redfish" : 0,
"Winter Flounder" : 0
}

gameOver = False
while not gameOver:
print("possession limits")
print(possessionLimit)
print("This is the number of fish you have caught")
print(fishTally)
print("maximum length allowed for fish")
print(maxFishLength)
fishLength = input ('How long is the fish you caught (inches)?: ') 
print('Alewife (a), Acadian Redfish (ar), Windowpane Flounder (wf) (or e to exit):')
item = input('Can I keep the fish? Type the one or two-letter code to find out: ')
if item == 'e':
gameOver = True
elif item == 'a':
print("You are going to have to throw this one back. Try Again.")
elif item =='ar':
print('You can keep this fish!')
fishTally['Acadian Redfish']+=1
elif item == 'wf':
print('Your fish is ', fishLength, ' long')
if fishTally['Winter Flounder'] < possessionLimit["Winter Flounder"]:
if fishLength ['Winter Flounder'] <= maxFishLength['Winter Flounder']:
print() 
print('You can keep this fish!')
fishTally['Winter Flounder'] +=1
else:
print('You have the incorrect fish length')

else:
print('You have caught your limit')
print()

fishLength不是字典,所以不能使用fishLength['Winter Flounder']。你需要把它转换成一个数字并进行比较。

if float(fishLength) <= maxFishLength['Winter Flounder']:

最新更新