首页 > 解决方案 > 蟒蛇水果机

问题描述

我正在为随机选择符号的水果机编写代码,但无法找出我哪里出错了。这是代码:

import random

credit  = 100

def roll(credit):

    sym1 = random.choice(symbols)
    sym2 = random.choice(symbols)
    sym3 = random.choice(symbols)
    print("---Symbols---")
    print(sym1, sym2, sym3, "\n")
    if (sym1 == sym2 or sym1 == sym3 or sym2 == sym3) and not(sym1 == sym2 == sym3):
        print("Winner! +50p")
        credit += 50
        return credit

    elif (sym1 == sym2 == sym3) and sym1 != "Bell":
        print("Winner! +£1")
        credit = credit + 100
        return credit

    elif (sym1 == sym2 == sym3) and (sym1 == "Bell"):
        print("Jackpot! +£5")
        credit = credit + 500
        return credit

    elif (sym1 == sym2 == "Skull" or sym1 == sym3 == "Skull" or sym2 == sym3 == "Skull") and not(sym1 == sym2 == sym3):
        print("Two Skulls! -£1")
        credit = credit - 100
        return credit

    elif (sym1 == sym2 == sym3) and sym1 == "Skull":
        print("Three Skulls! Lose all credit")
        credit = 0
        return credit

    else:
        print("Loser!")




symbols = ["Cherry", "Bell", "Lemon", "Orange", "Star", "Skull"]
print("You have", credit, "p", "\n")


while True:
    print("")
    play = input("Roll costs 20p. Would you like to roll? yes/no: ")
    print("")
    if play == "yes" or "y" or "Yes" or "Y":
        if credit >= 20:
            roll(credit)
            credit -= 20
            print("Credit is", credit, "p")

    else:
        print("You do not have enough money to roll!")

这里的问题是,当一个人获胜时,信用不会添加到信用变量中。然而,20p 总是被拿走。我会非常感谢这里的一些帮助。

谢谢

标签: python

解决方案


您这样做roll(credit)但不将值分配回信用。你需要做credit = roll(credit)


推荐阅读