0

このpythonコードに問題がありました。コーディングを始めたばかりで、なぜ機能しないのかわかりません。ループの繰り返しを止めることができません。何を入力しても add_item 関数が開始されます。任意のヒント?

supply = { #Creates blank dictionary
}
print "Would you like to add anything to the list?"

def add_item(*args):     #Adds a item to the supply dictionary
    print "What would you like to add?"
    first_item = raw_input()
    print "How much does it cost?"
    first_price = raw_input()
    supply[first_item] = float(first_price)

response = raw_input()

while response == "yes" or "Yes" or "YES":
    if response == "yes" or "Yes" or "YES": #Added this because it wasn't working, didn't help
        add_item()
        print "Alright, your stock now reads:", supply
        print "Would you like to add anything else?"
        response = raw_input()
    elif response == "no" or "No" or "NO":
        print "Alright. Your stock includes:" 
        print supply
        break
    else:
        print "Sorry I didn't understand that. Your stock includes:" 
        print supply
        break

print "Press Enter to close"
end_program = raw_input()
4

1 に答える 1

6

あなたはどのように機能するかについて混乱しているようですor

元の式は次のように書き直すことができます。

(response == "yes") or ("Yes") or ("YES")

つまり、等値式と残りの 2 つの文字列式のそれぞれの 3 つの式の真偽を測定しました。"Yes"とevaulate の両方"YES"が true であるため、(多かれ少なかれ) 次のようになります。

while (response == "yes") or True or True:

常に true と評価されるため"Yes"、while 式は常に true でした。

試す:

while response in ( "yes" , "Yes" , "YES"):

または、さらに良い:

while response.lower() == "yes":
于 2013-10-18T02:08:28.490 に答える