Python水果机

时间:2018-10-26 10:26:50

标签: python

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

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

谢谢

2 个答案:

答案 0 :(得分:1)

您进行了roll(credit)的操作,但没有将值分配回功劳。您需要执行credit = roll(credit)

答案 1 :(得分:0)

也:

  1. else函数中的roll子句中添加return语句,否则游戏在第一次失败后就结束了,因为您会得到TypeError: unsupported operand type(s) for -=: 'NoneType' and 'int'

  2. 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