Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 蟒蛇水果机_Python - Fatal编程技术网

Python 蟒蛇水果机

Python 蟒蛇水果机,python,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,

我正在为一台水果机编写一个代码,它以随机方式选择符号,却无法找出我的错误所在。代码如下:

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!")
这里的问题是,当一个人获胜时,信用没有添加到信用变量中。然而,20便士总是被拿走。我将非常感谢您的帮助

谢谢

您可以
滚动(信用)
,但不要将值重新分配给信用。您还需要执行以下操作:

  • roll
    函数中的
    else
    子句中添加return语句,否则游戏在第一次失败后就结束了,因为您将得到
    TypeError:unsupported操作数类型for-=:'NoneType'和'int'

  • while
    循环的
    else
    子句中添加一个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
    

    另外
    如果在{“是”、“y”、“是”、“y”}中播放:
    如果你想进入else子句,这可能更适合在感谢所有人的回复:)