通过gam实现硬币计数一致性

2024-06-02 08:35:15 发布

您现在位置: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()

Tags: thetoinyou游戏硬币课程print
2条回答

你应该有一些玩家状态变量来传递给你的每个“区域”函数。它可以简单到一句话:

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)
#... 
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)

相关问题 更多 >