未正确调用字典
我有一个老虎机程序,我试图使它不是很好地工作。我认为问题在于我从字典里取错了词。没有给出正确的硬币数量,我不确定为什么。它的规格是:
一人游戏以100枚硬币开始。
一轮5个硬币。
继续玩,直到你没有剩余的钱,或者玩家选择'套现'
每一步都会随机旋转这三个微调器。每个旋转器有5个符号:贝尔、樱桃、香蕉、美元、骷髅。
3个水果=50个硬币
2个同样的水果=10个硬币
3个铃铛=1000枚硬币
2个铃铛=100枚硬币
3美元=500枚硬币
2美元=50枚硬币
1美元=1枚硬币
金条意味着所有的钱都乘以10
如果任何旋转手柄上出现头骨,你可能赢不到任何钱。
以下是代码:
选择 | 换行 | 行号
- import tkinter as tk
- import random
- print("Welcome to the Virtual Slot Machine")
- global coins
- coins = 100
- symbols = ["bells", "cherries", "bananas", "dollars", "skull"]
- done = False
- def sort(lists):
- for i in range(len(lists)-1):
- for x in range(len(lists)-1-i):
- if lists[x] < lists[x + 1]:
- lists[x], lists[x + 1] = lists[x + 1], lists[x]
- return lists
- def open_file():
- f = open("highscores.txt","r")
- line = f.readlines()
- f.close()
- list1 = [int(i) for i in line[0].split(",")]
- ordered = sort(list1)
- return ordered
- print("The highscores so far from higherst to smallest are: ")
- print(*open_file())
- def Match(slot1, slot2, slot3):
- if slot1 == slot2 and slot2 == slot3:
- return slot1, 1
- elif slot1 == slot2:
- return slot1, 0
- elif slot2 == slot3:
- return slot2, 0
- elif slot1 == slot3:
- return slot3, 0
- return None, None
- def turn(coins):
- coins -= 5
- fruits = ["cherries", "bananas"]
- slot1 = random.choice(symbols)
- slot2 = random.choice(symbols)
- slot3 = random.choice(symbols)
- slots = [slot1, slot2, slot3]
- reward_dict = {"dollars" : [50, 500], "bells": [100, 1000]}
- for fruit in fruits:
- reward_dict.update({fruit: [10, 50]})
- if random.randint(0,10000) == 1:
- slot3 = "Gold Bar"
- print("You got {} {} {}".format(slot1,slot2,slot3))
- if "skull" not in slots:
- match, number = Match(slot1, slot2, slot3)
- if match != None:
- coins += reward_dict[match][number-1]
- print("You got {} {}! +{} coins!".format(number+2, match, reward_dict[match][number]))
- else:
- print("Unlucky, you got a skull, you lose!")
- if slot1 == "dollar" or slot2 == "dollar" or slot3 == "dollar":
- coins += 1
- if slot3 == "Gold Bar":
- print("Jackpot! All coins times 10!")
- coins = coins*10
- return coins
- while coins > 0 and done == False:
- print("You have {0} coins".format(coins))
- play = input("Do you want to play a round? It costs 5 coins? y/n ")
- coins_left = coins - 5
- if play == "y" and coins_left > 0:
- coins = turn(coins)
- else:
- if coins_left < 0:
- print("Sorry, you do not havwe enough money")
- print("You ended with {0} coins".format(coins))
- else:
- print("Ok, thanks for playing")
- print("You ended with {0} coins!".format(coins))
- done = True
谢谢!