私は与えられたリストの数字の合計に基づいて決定を下すためにこのプログラムを構築しています。私はまた、必要なロジックceratinテスト与えられた関数で数値を合計すると、それは正しい答えを与えるが、値をテストするステートメントが正常に動作しない場合、またはif文間違って実行します:float sum(list)ValueError
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()
を最初から南に3回行くとエラーが表示されます。2回はパントリーの東に向かい、1回は何もしません。私は非常に失われています この時点でプログラムは3つのより類似したelif文が続きますが、入力を最初に与えると無視され、2回目は素数を含むものを除いて間違った動作を実行します1/11の割合;他のすべてのエリフは、増加する素数{1/11、1/13、1/17、1/19}を使用します。私はプログラミングの経験がほとんどなく(3か月)、最初の言語としてPythonを学んでいます。どんな助けでも大いに喜ぶでしょう。
enumerationを使用する代わりに変更することをお勧めします。 –
@EliSadoffコメントに追加すると、sum関数は無限ループ上で再帰的に呼び出されます。ほとんどの場合、実行はif文に達しません –
申し訳ありませんが、間違ってマークされています。 sum()関数、どちらの方法でも動作しません –