0

レースの予想結果と賭けのリストを受け取る関数を作成しようとしています。したがって、たとえば、馬 1 が 1 位、馬 3 が 2 位などの結果 [1,3,4,2] を予測できます。この予測は、ランダム化/シャッフルされたリストと比較されます。各リスト内の両方のアイテムを相互に比較するためにいくつかの if ステートメントを設定しましたが、最終的にはすべての配置が正しかったとしても正しくありませんでした。ハマった!!!

def horseRace(gList,bet):

    import random

    a = [1,2,3,4]
    random.shuffle(a,random.random)
    i = 0
    x = 0
    while i < len(gList):
        if gList[i] == a[0]:
            x = x + 1
        if gList[i] == a[1]:
            x = x + 1
        if gList[i] == a[2]:
            x = x + 1
        if gList[i] == a[3]:
            x = x + 1
        else:
            x = x
        i = i + 1
    print(x)
    print(a)
    print(gList)
    if x == 4:
        money = bet*2
        print("You guessed 4 position(s) correctly and won $ %d !"%money)
    if x == 0:
        money = 0.00
        print("You guessed no position(s) correctly and won $ %d !"%money)
    if x == 1:
        money = bet
        print("You guessed 1 position(s) correctly and won $ %d !"%money)

    if x == 2:
        money = bet
        print("You guessed 2 position(s) correctly and won $ %d !"%money)

    if x == 3:
        money = bet
        print("You guessed 3 position(s) correctly and won $ %d !"%money)
4

3 に答える 3

2

whileループは次のようになります

  while i < len(gList):
        if gList[i] == a[i]:
            x = x + 1
        else:
            x = x
        i = i + 1

iこのようにして、 のセル位置のgList値を の同じセル位置の値と比較しますa。現在、コードは基本的に、値が次のいずれかと等しいx限り、に 1 を追加するように指示しています。gList[i]

a[1]a[2]a[3]a[4]

もちろんそれはどちらでしょう

于 2013-09-04T04:12:16.643 に答える
1

for ループを使用して、正しい推測の数をカウントすることを検討してください。gListaが同じサイズであると仮定します。

for (gList_i, a_i) in zip(gList, a):
    if g_List_i == a_i:
        x += 1

また、正しく推測された位置の数を入力として受け取るメッセージの関数かもしれません。

def win_message(nwins):
    if nwins == 0:
        money = 0
    elif nwins == 4:
        money = 2*bet
    else: # For nwins equals 2 or 3
        money = bet

    if nwins > 1:
        plural = "s"
    else:
        plural = ""
    print("You guessed %d position%s correctly and won $ %d !" % (nwins, plural, money))
于 2013-09-04T04:03:40.253 に答える
0

よりPython的な方法でそれを行うことができ、1行で正しい推測の数を数えます:

nCorrect = len(filter(lambda x: x[0]==x[1], zip(randomized,gList)))

これが完全なコードです。下で説明します。

import random
def horseRace(gList,bet):
    randomized = [1,2,3,4]
    random.shuffle(randomized,random.random)
    nCorrect = len(filter(lambda x: x[0]==x[1], zip(randomized,gList)))

    def getResult(bet,nCorrect):
        if nCorrect==0:
            return 0
        elif nCorrect==4:
            return 2*bet
        else:
            return bet

    outcome = getResult(bet,nCorrect)

    print 'Randomized order: ' + randomized
    print 'Your guess: ' + bet
    print 'You guessed %d position(s) correctly and won $%d!' % (nCorrect,outcome)

getResult結果の計算方法に応じて、方法を変更する必要があります。

この行について説明します。

nCorrect = len(filter(lambda x: x[0]==x[1], zip(randomized,gList)))

したがって、最初にrandomizedandgListをタプルのリストに結合します。

gList = [1,2,3,4]
randomized = [1,3,2,4]
print zip(randomized,gList)
    [(1,1), (2,3), (3,2), (4,4)]

次に、filter条件に一致する要素のみを取得します。この場合、条件は、タプルの最初の要素がタプルの 2 番目の要素と等しいことです ( x[0]==x[1])

print filter(lambda x: x[0]==x[1], zip(randomized,gList))
[(1,1), (4,4)]

次に、正しい推測の数である配列の長さを返します。

于 2013-09-04T05:04:58.153 に答える