0

我正在尝试创建一个函数,它包含一个列表,我给它我预测的比赛结果和我的赌注。因此,例如,我可以预测结果 [1,3,4,2],其中马 1 排在第 1 位,马 3 排在第 2 位,等等……这个预测将与随机/打乱列表 a 进行比较。我已经设置了几个 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

That way, you're comparing the value in cell position i in gList to the value in the same cell position in a. Right now, your code essentially says to add one to x as long as the value gList[i] is equal to any of:

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

which of course it wil be

于 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

You can do it in more Pythonic way, and count the number of correct guess in one line:

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

Here is the full code, I'll explain in the bottom:

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)

You should change the getResult method depending on how you want to calculate your outcome.

I'll explain this line:

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

So, first, it'll combine the randomized and gList into a list of tuples:

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

Then the filter will take only elements that match the condition. In this case the condition is that the first element of the tuple equals the second element of the tuple (x[0]==x[1])

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

Then we return the length of the array, which is the number of correct guess.

于 2013-09-04T05:04:58.153 回答