【问题标题】:How do I fix my comparison test in Python?如何在 Python 中修复我的比较测试?
【发布时间】:2021-09-03 18:29:26
【问题描述】:

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()

【问题讨论】:

  • 您的 github 链接无效。请在此处发布minimal reproducible example,而不是在其他站点。
  • &lt;= 条件是正确的,如果你想说没问题。你确定变量是正确的吗?
  • Barmar,感谢您的回复。那么,我们不能通过链接发布代码吗?我认为我的 github 设置为私有。将尝试弄清楚如何将其公开。不,我不确定变量是否正确。 . .我从github发布了相关代码。
  • 包括您遇到的错误将非常有帮助,例如,我认为在这种情况下,错误可能是 TypeError: string indices must be integers,它可以很好地了解可能出现的问题
  • Tadhg McDonald-Jensen,感谢您的回复。这是错误: if fishLength ['Winter Flounder']

标签: python comparison-operators


【解决方案1】:

fishLength 不是字典,所以不能使用fishLength['Winter Flounder']。您需要将其转换为数字并进行比较。

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

【讨论】:

  • 谢谢你,巴尔玛。我会试试的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-07
  • 2018-12-12
  • 2012-06-18
  • 1970-01-01
相关资源
最近更新 更多