Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 浮点和(列表)值错误_Python - Fatal编程技术网

Python 浮点和(列表)值错误

Python 浮点和(列表)值错误,python,Python,我一直在构建这个程序,根据给定列表中的数字总和做出决策。当我对测试ceratin所需逻辑的给定函数中的数字求和时,它给出了正确的答案,但测试该值的if语句不能正常工作,或者将执行错误的if语句: import random from decimal import Decimal from decimal import getcontext def language_equate(sentence_parsing): both = ["and", ",", "then", "next"

我一直在构建这个程序,根据给定列表中的数字总和做出决策。当我对测试ceratin所需逻辑的给定函数中的数字求和时,它给出了正确的答案,但测试该值的if语句不能正常工作,或者将执行错误的if语句:

import random
from decimal import Decimal
from decimal import getcontext


def language_equate(sentence_parsing):
    both = ["and", ",", "then", "next"]
    ignore = ["don't", "not"]
    travel = ["move", "go", "travel"]
    dial = ["rotate", "turn", "spin", "twist"]
    lever_positive = ["push", "back"]
    lever_negative = ["pull", "forward"]
    north = ["north", "up", "forward"]
    east = ["east", "right"]
    west = ["west", "left"]
    south = ["south", "down", "backward"]
    modded_sentence = []
    getcontext().prec = 20
    for i, word in enumerate(sentence_parsing):
        if word.lower() in both:
            modded_sentence.append(Decimal(99))
        elif word.lower() in ignore:
            modded_sentence.append(Decimal(-20))
        elif word.lower() in travel:
            modded_sentence.append(Decimal(1))
        elif word.lower() in dial:
            modded_sentence.append(Decimal(1/3))
        elif word.lower() in lever_positive:
            modded_sentence.append(Decimal(1/5))
        elif word.lower() in lever_negative:
            modded_sentence.append(Decimal(1/7))
        elif word.lower() in north:
            modded_sentence.append(Decimal(1/11))
        elif word.lower() in east:
            modded_sentence.append(Decimal(1/13))
        elif word.lower() in west:
            modded_sentence.append(Decimal(1/17))
        elif word.lower() in south:
            modded_sentence.append(Decimal(1/19))
    return modded_sentence


def language_evaluate(sentence_parsing):
    modded_sentence = language_equate(sentence_parsing)
    a = sum(modded_sentence)
    return a


def entrance(dial, lever, win, current_room, EPSILON):
    print("You are in the entrance.")
    print("Go north to try the closed door")
    print("Go east to the kitchen")
    print("Go west to the pantry")
    print("Go south to exit")
    print("What would you like to do?")
    sentence_parsing = input().split(" ")
    go = language_evaluate(sentence_parsing)
    # Exclusive Disjunction Answer Bank
    answer_bank = ["Y", "y", "Yes", "yes", "YES"]
    if abs(go - Decimal(1/11)) <= EPSILON or abs(go - Decimal(12/11)) <= EPSILON: # separate statements
        if dial == "red" and lever == "backward":
            win = True
            current_room = ""
        else:
            print("Sorry the door remains locked")
    elif abs(go - Decimal(1/13)) <= EPSILON or abs(go - Decimal(14/13)) <= EPSILON:
        current_room = "kitchen"
    elif Decimal(1/17) or Decimal(18/17):
        current_room = "pantry"
    elif abs(go - Decimal(1/19)) <= EPSILON or (go - Decimal(20/19)) <= EPSILON:
        print("Are You sure you want to quit? [Y/N]")
        give_up = input()
        if give_up in answer_bank:
            print("Ok, maybe next time")
            raise SystemExit
    else:
        print("Sorry I don't understand that")
    return win, current_room


def kitchen(lever, lever_position, current_room, EPSILON):
    print("You are in the kitchen.")
    print("Go north to move the lever the %s position" % lever)
    print("Go west to the entrance")
    print("What would you like to do?")
    sentence_parsing = input().split(" ")
    go = language_evaluate(sentence_parsing)
    print(go)
    if abs(go - 16/55) <= EPSILON or abs(go - 71/55) <= EPSILON or abs(go - 1/5) <= EPSILON:
        lever = lever_position[1]
    elif abs(go - 18/77) <= EPSILON or abs(go - 95/77) <= EPSILON or abs(go - 1/7) <= EPSILON:
        lever = lever_position[0]
    elif abs(go - 1/17) <= EPSILON or abs(18/17) <= EPSILON:
        print("test works")
        current_room = "entrance"
    else:
        print("Sorry I don't understand that")
    return lever, current_room


def pantry(dial, current_room, EPSILON):
    print("You are in the pantry.")
    print("South of you is a three coloured dial, with the color %s glowing" % dial)
    print("Go east to the entrance")
    print("Go south to turn the dial")
    print("What would you like to do?")
    sentence_parsing = input().split(" ")
    go = language_evaluate(sentence_parsing)
    print(int(go))
    if abs(go - 1/19) <= EPSILON or abs(go - 20/19) <= EPSILON or abs(go - 22/57) <= EPSILON or abs(go - 79/57) <= EPSILON or abs(go - 1/3) <= EPSILON:
        dial += 1
        if dial == 3:
            dial = 0
    elif abs(go - 1/13) <= EPSILON or abs(14/13) <= EPSILON:
        current_room = "entrance"
    else:
        print("Sorry I don't understand that")
    return dial, current_room


def main():
    EPSILON = Decimal(0.00000000001)
    current_room = "entrance"
    dial_color = ["green", "blue", "red"]
    dial = dial_color[random.randrange(3)]
    lever_position = ["forward", "backward"]
    lever = lever_position[random.randrange(2)]
    win = False
    while win is False:
        while current_room is "entrance":
            entrance(dial, lever, win, current_room, EPSILON)
            win, current_room = entrance(dial, lever, win, current_room, EPSILON)
        while current_room is "kitchen":
            kitchen(lever, lever_position, current_room, EPSILON)
            lever, current_room = kitchen(lever, lever_position, current_room, EPSILON)
        while current_room is "pantry":
            pantry(dial, current_room, EPSILON)
            dial, current_room = pantry(dial, current_room, EPSILON)
main()
随机导入
从十进制输入十进制
从decimal导入getcontext
def语言等同(句子分析):
两者都=[“和”、“、”然后“、”下一步“]
忽略=[“不”、“不”]
差旅=[“移动”、“前进”、“差旅”]
刻度盘=[“旋转”、“旋转”、“旋转”、“扭转”]
操纵杆(正)=[“推”,“后”]
杠杆负=[“拉动”,“向前”]
北=[“北”、“向上”、“向前”]
东=[“东”,“右”]
西=[“西”,“左”]
南部=[“南部”、“向下”、“向后”]
修饰句=[]
getcontext().prec=20
对于i,枚举中的单词(句子分析):
如果word.lower()同时出现在以下两种情况:
修饰句。附加(十进制(99))
忽略中的elif word.lower():
修饰句。附加(十进制(-20))
行程中的elif word.lower()
修饰句。附加(十进制(1))
拨盘中的elif word.lower()符号:
修饰句。附加(十进制(1/3))
elif word.lower()在杠杆_正极中:
修饰句。附加(十进制(1/5))
elif word.lower()在lever_负片中:
修饰句。附加(十进制(1/7))
elif word.lower()位于北部:
修饰句。附加(十进制(1/11))
东部的elif word.lower():
修饰句。附加(十进制(1/13))
elif word.lower()在西方:
修饰句。附加(十进制(1/17))
elif word.lower()在南部:
修饰句。附加(十进制(1/19))
返回修饰句
def语言评估(句子分析):
modded_句子=语言等同(句子解析)
a=总和(修饰句)
归还
def入口(刻度盘、控制杆、win、当前_室、ε):
打印(“你在入口处。”)
打印(“向北尝试关闭的门”)
打印(“向东到厨房”)
打印(“向西到食品储藏室”)
打印(“向南退出”)
打印(“您想做什么?”)
句子解析=输入().split(“”)
go=语言评估(句子分析)
#排他析取应答库
答案(bank=[“Y”,“Y”,“Yes”,“Yes”,“Yes”]

如果abs(go-Decimal(1/11))代码中存在大量错误。当您三次输入
“south”
作为输入时,让我们看看您描述的问题的原因

入口
功能有一个测试严重中断:

elif Decimal(1/17) or Decimal(18/17):
这永远都是真的,所以如果你达到了这个条件(不去北方或东方,这是之前测试过的),你总是会去食品储藏室

pantry
函数也有类似的问题,在从解析代码中得到的值减去之前,您没有将部分分数转换为
Decimal
。这会导致您看到的异常

之所以有两次
条目
,是因为您的
main
函数总是调用当前房间的函数两次(在两个连续的行上)。第一次做出的选择将被忽略

我看到的代码的其他部分可能还有更多的错误

通过使用分数作为解析输入文本的值,您的任务变得比需要的困难得多。更好的方法是使用2的整数幂,并使用按位or(
|
)组合它们。例如,如果单词
move
被解析为
1
2**0
)和
south
被解析为
16
2**4
),那么如果go==16或go==17(不需要类型转换或epsilon),那么
south
向南移动的测试可能是
。如果您不关心额外的字,可以使用位and(
&
)只测试一个术语:
If go&16


你也可以考虑使用<代码> EnUM <代码>,如果你使用一个Python的版本,它包含了<代码> EnUM 模块(它在Python 3.4中添加)。

< P>这是你的程序的问题:

  • 当您应该使用时,您可以使用相同的测试,例如在这里

    while current_room is "entrance":
    
    is
    操作符告诉您to对象在内存中是否有相同的位置,而不是它们是否有相同的值,例如,对于不可变对象(如字符串),它们通常可能是相同的,但您不能指望这一点

    >>> "asfdvfgbgdf" is "".join("asfdvfgbgdf")
    False
    >>> 
    
    使用
    ==
    时请给出正确答案

    >>> "asfdvfgbgdf" == "".join("asfdvfgbgdf")
    True
    >>> 
    
    将所有
    都更改为
    =

  • 您不需要将布尔值或任何其他值与
    True
    False
    进行比较。在python中,当在中使用对象时,该对象的行为为True或False

    值被解释为false:
    false
    None
    、所有类型的数字零以及空字符串和容器(包括字符串、元组、列表、字典、集合和冻结集)。所有其他值都被解释为true。用户定义的对象可以通过提供
    \uuu bool\uuu()
    方法自定义其真值

    比如这个

    while win is False:
    
    for word in sentence_parsing:
        word = word.lower()
        if word in both:
            ...
        elif word in ignore:
            ...
        ...
    
    换成

    while not win:
    
  • main
    函数中,调用其他函数两次,这会导致第一次忽略结果,因为您不使用它

    就像在这里一样

    entrance(...)
    win, current_room = entrance(...)
    
    厨房和餐具室也一样。删除每个问题的第一个调用以解决该问题

  • 在食品储藏室功能,这里

    elif abs(go - 1/13) <= EPSILON or abs(14/13) <= EPSILON:
    
    这是什么意思?到第2点时,这总是正确的,因为这些数字中没有一个是零,所以如果另一个检查失败,这将始终通过

  • 在for循环中的
    语言中

    for i, word in enumerate(sentence_parsing):
    
    您从未使用过
    i
    ,因此没有理由使用enumerate,所以
    for i, word in enumerate(sentence_parsing):
    
    for word in sentence_parsing:
    
    for word in sentence_parsing:
        word = word.lower()
        if word in both:
            ...
        elif word in ignore:
            ...
        ...
    
    input().lower()
    
    input().lower().split()