硬币通过游戏计算一致性

时间:2015-09-23 16:51:55

标签: python

我正在通过学​​习Python艰难的方式学习python。我在第35课,你写了一个简短的“游戏”。

从课程开始,我正在制作自己的小游戏,以扩展我对课程这一部分的理解。我正在尝试购买用于购买东西,拾取,丢弃等的硬币。我无法弄清楚如何使硬币计数保持一致(即如果你买东西,它会带走x金币,下次你去买东西时,你也只剩下其余部分了。

以下代码部分示例。我已经尝试了所有我知道怎么做的事情(这显然在这一点上并不多!)但是当再次调用该函数时(购买剑或盾牌之后),硬币计数总是会重置。

def bazaar1():
print "\"Welcome to the Hyrule Bazaar\", the storekeeper mumbles."
print "What would you like to buy? We have:\n\nSwords (10c)\nShields (10c)\nBows (10c)\nArrows (10 for 10c)\n"

bazaar_c = raw_input("> ")
if "sword" in bazaar_c:
    print "You got the Hero Sword!\n"
    bazaar1()
elif "shield" in bazaar_c:
    print "You got the Hyrule Shield.\n"
    bazaar1()
elif "leave" in bazaar_c:
    hyruletown()
elif "exit" in bazaar_c:
    hyruletown()
else:
    print "Sorry, that's not an option.\n"
    bazaar1()

2 个答案:

答案 0 :(得分:0)

你应该有某种玩家状态变量传递给你的每个区域'功能。它可以像字典一样简单:

player_data = {
 'hearts': 3,
 'coins': 10,
 'items': []
}

然后将其传递给您的每个职能部门:

def bazaar1(player_data):
  print "\"Welcome to the Hyrule Bazaar\", the storekeeper mumbles."
  print '\n'.join(["What would you like to buy? We have:\n",
                   'Swords (10c)',
                   'Shields (10c)',
                   'Bows (10c)',
                   'Arrows (10 for 10c)'])

  bazaar_c = raw_input("> ")
  if "sword" in bazaar_c:
    if player_data['coins'] >= 10:
        print "You got the Hero Sword!\n"
        player_data['inventory'].append('sword')
        player_data['coins'] -= 10
    else:
        print "You don't have enough coins"
    bazaar1(player_data)
#... 

答案 1 :(得分:-1)

again=True

Total=0

Wrongs=0

Corrects=0


Total_bags=0

Wrong_bags=0

Correct_bags=0

coins=['1p','2p','5p','10p','20p','50p','£1','£2']

bag_value=[1.00,1.00,5.00,5.00,10.00,10.00,20.00,20.00]

bag_weight=[356,356,325,325,250,160,175,120]

coins_len=len(coins)

Master_list=list()

CoinCount_list=list()

Tile_list=['Name','Coin','Weight','Accurate']

Master_list.append(Tile_list)

with open("Master.txt","w") as f: 
    f.write("{}".format(Master_list))

while True:
again=True


what=input("Would you like to add a bag")


if what=='Yes':

    name=input("What is your name")

    CoinCount_list.append(name)

    with open("CoinCount.txt","w") as f: 
        f.write("{}".format(CoinCount_list))



    Percent=Corrects*Wrongs/100



    CoinCount_list.append(Percent)

    with open("CoinCount.txt","w") as f: 
        f.write("{}".format(CoinCount_list))

    Percent=0

    Wrongs=0

    Corrects=0 

    while again:

硬币

        coin_input=input("What type of coin do you have")
        for i in range (0,coins_len):






            if coin_input==coins[i]:
                Coin=coins[i]
                valid=input("That is valid, is that what you wanted")

                if valid=="Yes":


                    CoinCount_list.append(coins[i])

                    with open("CoinCount.txt","w") as f: 
                        f.write("{}".format(CoinCount_list))

重量

                    weight=int(input("What is the weight of the bag"))


                    CoinCount_list.append(weight)

                    with open("CoinCount.txt","w") as f: 
                        f.write("{}".format(CoinCount_list))

                        if weight == bag_weight[i]:

                            Total_bags=Total_bags+1

                            again=input("Thank you would you like to add another bag")

                            CoinCount_list.append('Correct')

                            with open("CoinCount.txt","w") as f: 
                                f.write("{}".format(CoinCount_list))

                            Total=bag_value[i]+Total
                            Correct_bags=Correct_bags+1

                            Corrects=Corrects+1

                            if again== 'Yes':
                                a='b'



                            else:
                                again=False


                                Master_list.append(CoinCount_list)

                                with open("Master.txt","w") as f: 
                                    f.write("{}".format(Master_list))







                        elif weight > bag_weight[i]:
                            print("You have too many coins")

                            Wrong_bags=Wrong_bags+1
                            Total_bags=Total_bags+1

                            Wrongs=Wrongs+1



                        elif weight < bag_weight[i]:
                            print("You have too few coins")

                            Wrong_bags=Wrong_bags+1
                            Total_bags=Total_bags+1

                            Wrongs=Wrongs+1




    else:
        print("")
        print("  We have check",Total_bags," ")
        print(" ",Correct_bags,"Correctly")
        print(" ",Wrong_bags,"Incorrectly")
        print("  We have raised",Total," ")
        print("")
        with open("Master.txt") as f:
            rd=f.readlines()
        print (rd)