Click here to Skip to main content
15,918,178 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
To preface this, I don't want it to come across as if I want to be tutored. I've currently been struggling to get this piece of code the way it needs to be and the only available assistance i can get at the moment is from the internet. Of course, if this is the wrong place for such a post, do feel free to take it down, i completely understand :)

I'm giving a go at making a detection system and scoreboard for a text based robbery game whereby certain contexts increase detection. For example, if a gun is fired, it alerts the guards increasing detection, however if a knife is used, nothing happens to the detection level. Depending on the level of detection the user completes the game with, they leave with a certain amount of cash.

What i currently have is that the code iterates over a variable 'inventory' which stores the items that a user has picked up along away. When the user uses the item, it's no longer in the inventory for the duration that it's used for. The code then evaluates a condition to check if the item is in the inventory at that point. If it's not, it adds the 'discrete' value of the item being used to a variable 'discretion' with a predefined value. If 'discretion' falls below a certain value, it calls a function 'try_gain()' which prompts the user to play again.

Here's the code i have so far. This isn't what I've settled for, so if you have any advice on improving the code, it would go a long way:

    user_input = input('Would you like to play again')
    if 'yes'.lower() in user_input:


def discretion(items):
discretion = 0
inventory = []
for x,a in items.items():
    if x not in inventory:
        discretion += a['discrete']


if discretion < 0:

return (f'Your detection level is {discretion}')
Here are a few sample dictionaries:

hand_gun = {'id':'handgun','discrete':20}
rifle_ = {'id':'rifle','discrete':50}
items = {'handgun':hand_gun,'rifle':rifle_}

What I have tried:

So far what I've done is to iterate over items and check if an item is not in inventory. If it is not (e.g when the pistol is used) a change is made to discretion according to the discrete value stored by the particular item in its dictionary. The problem I've come across with this is that 'Inventory' should be (as in the other code of the game too) a global variable instead of being enclosed in this function. On top of that, I'm struggling to establish a solution on constantly representing the status of 'detection' of the player throughout the game. Of course, not all questions need to be answered but any help would go a long way :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900