Reputation: 1
Im building a simple landscaper game and cant program wont progress to the next available tool in the list. It progresses to scissors but wont progress to lawnmower. I'll include all code since its pretty short.
Here is my list of tools.
tools = [
{
'name': 'Rusty scissors',
'cost': 5,
'profit': 5
},
{
'name': 'Old Lawn Mower',
'cost': 25,
'profit': 50
},
{
'name': 'Battery Powered Mower',
'cost': 250,
'profit': 100
},
{
'name': 'Team of Students',
'cost': 500,
'profit': 250
}
]
Here is my player/landscaper
player = {
'money': 0,
'tools': [],
'current_tool': {
'name': 'Teeth',
'cost': 0,
'profit': 1
}
}
and here are my functions of gameplay
def buy_tool():
for tool in tools:
if player['current_tool'] != tool and player['money'] >= tool['cost']:
player['current_tool'] = tool
player['money'] -= tool['cost']
cut_grass()
def check_money():
for tool in tools:
if player['money'] >= tool['cost']:
buy_tool()
else:
cut_grass()
def cut_grass():
print('You currently have $' + str(player['money']) + ' and are using your ' + player['current_tool']['name'] + '.')
start_cutting = input('Are you ready to cut grass? (y/n)')
if start_cutting == 'y':
player['money'] += player['current_tool']['profit']
check_money()
cut_grass()
Upvotes: 0
Views: 85
Reputation: 77857
Your code searches for the first tool that you can afford. As soon as it finds one -- which is always scissors -- it stops the search and recurs (bad design) to cut_grass
.
for tool in tools:
if player['current_tool'] != tool and player['money'] >= tool['cost']:
player['current_tool'] = tool
player['money'] -= tool['cost']
break
cut_grass()
You have to change your logic to find the most expensive tool. The easiest way to do this is to reverse the order of your tools list.
Upvotes: 2