加老虎机滚入循环的结果

2024-10-04 05:26:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一个像吃角子老虎机一样工作的程序。在我的循环中,我试图根据一个玩家的旋转结果来决定他们获得多少学分。然而,假设一个玩家掷骰并赢得300个信用,当被要求再次旋转时,如果他们又赢了一次,它就不记得前一次旋转的结果。在

我知道这是一个简单的修复,但基本上每次循环返回时,它都会忘记更新后的总数。我对编程还不熟悉,这些东西对我来说简直就是地狱。在

下面是循环部分的内容:

cashPool = 500

import random

# Assigning wheel properties
wheel1 = ['Candy','Ghost','Pumpkin','Cat','Zombie','Witch','Witch','Cat','Ghost','Candy']
wheel2 = ['Candy','Ghost','Pumpkin','Cat','Zombie','Witch','Cat','Pumpkin','Ghost','Candy']
wheel3 = ['Candy','Ghost','Pumpkin','Cat','Zombie','Witch','Pumpkin','Candy','Candy','Ghost']

#loop to spin


def AskYesNo(question):
    tmp = input(question).lower()
    if tmp.startswith('y'):
        return True
    else:
        return False

i = 0

while True:
    spin1 = random.randint(3,4)
    spin2 = random.randint(3,4)
    spin3 = random.randint(3,4)
    print(str(wheel1[spin1])+ '\t' + str(wheel2[spin2]+ '\t' + str(wheel3[spin3])))
    i += 1
    if spin1==4 and spin2==4 and spin3==4:
        print('Congratulations! You have won 300 credits')
        result = cashPool + 300
        print('You currently have '+ str(result) +' credits')
    if spin1==5 and spin2==5 and spin3==5:
        print('Congratulations! You have won 250 credits')
        result = cashPool + 250
        print('You currently have '+ str(result)+ ' credits')
    if spin1==3 and spin2==3 and spin3==3:
        print('Congratulations! You have won 200 credits')
        result = cashPool + 200
        print('You currently have '+cashPool+' credits')
    if spin1==2 and spin2==2 and spin3==2:
        print('Congratulations! You have won 150 credits')
        result = cashPool + 150
        print('You currently have '+cashPool+' credits')
    if spin1==1 and spin2==1 and spin3==1:
        print('Congratulations! You have won 100 credits')
        result = cashPool + 100
        print('You currently have '+cashPool+' credits')
    if spin1==0 and spin2==0 and spin3==0:
        print('Congratulations! You have won 50 credits')
        result = cashPool + 50
        print('You currently have '+cashPool+' credits')
    AskYesNo('Would you like to spin? Enter y/n ')
    if False:
        break

而且,我百分之百地肯定有一种更简单的方法来计算每次旋转的结果,而不是一遍又一遍地做if语句。但我又是新来的,这超出了我的能力。我不能像程序员一样思考,我不知道人们是怎么做到的。在


Tags: andyouifhaveresultghostcreditsprint
2条回答

不要定义新的result变量,只需增加现有的cashPool变量。E、 g.:

if spin1==4 and spin2==4 and spin3==4:
    print('Congratulations! You have won 300 credits')
    cashPool += 300 # Here
    print('You currently have '+ str(result) +' credits')

首先,random.randint(3,4)只返回3或4。。。如果需要其余值,则需要为(0,4)。在

其次,这个部分不起作用,因为if False从来没有输入过。在

AskYesNo('Would you like to spin? Enter y/n ')
if False:
    break

你需要从那个函数中得到布尔值。在

^{pr2}$

其他一些建议包括不要重复太多。使它DRY

例如,从

Python: determine if all items of a list are the same item

def all_same(items, element):
    return all(x == element for x in items)

您可以提取一些变量常量,使用列表和一些函数编程来检查插槽。在

这样,你只需要玩数字,你就有了一个完全不同的游戏,只需要做最小的改变。在

total_slots = 3
max_range = 4
multiplier = 50

while True:
    i += 1
    spins = [random.randint(0, max_range) for _ in range(total_slots)]
    print('\t'.join(map(str, spins)))

    credits = 0
    for i in range(max_range + 1): # We loop over all spin possible values
        won = all_same(spins, i)
        if won:
            credits = (i + 1) * multiplier #  when i == 0, we add 50
            print('Congratulations! You have won {} credits'.format(credits))
            break #  can stop the for-loop

    cashPool += credits  #  if you didn't win, nothing is added
    print('You currently have {} credits'.format(cashPool))

    if AskYesNo('Would you like to spin? Enter y/n '):
        break

注意:我知道我没有包括wheel值。当您获得wheel列表中每个spin值的字符串后,all_same方法仍然有效。在

相关问题 更多 >