我正在为水果机编写代码,该代码以随机方式选择符号,并且无法找出我要去哪里。这是代码:

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总是被拿走。我会很感激这里的一些帮助。

谢谢

最佳答案

也:


else函数的roll子句中添加return语句,否则游戏在第一次失败后就结束了,因为您会得到TypeError: unsupported operand type(s) for -=: 'NoneType' and 'int'
else循环的while子句中添加break语句-否则,您必须在回答“否”后手动停止代码。


完整的代码,以及Farhan和Chris_Rands的建议如下所示:

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!")
        return credit

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 in {"yes","y","Yes","Y"}:
     if credit >= 20:
         credit = roll(credit)
         credit -= 20
         print("Credit is", credit, "p")

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

关于python - Python水果机,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53006689/

10-12 13:30