Click here to Skip to main content
15,949,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Imagine that you are a character who is in a video game. You have two hands with which you can hold at maximum two weapons (one per hand). Using object-oriented programming, have your character pick up the objects that they find in the wild. Have your character choose the weapon that has the maximum strength. The weapons should have the following attributes:

- Name (string)

- Strength (number)

The character should have the following attributes:

- Left_hand_weapon (weapon)

- Right_hand_weapon (weapon)

The character should have the following methods:

- Replace_weapon(w), where w is the weapon that was found. It will replace left hand or right hand weapon, depending on which weapon is weaker. If the weapon is weaker than both weapons the character is holding, the weapon will not be replaced.

- List_weapons(), which will list the name and strength of the weapons.



some tip:

create a class called Character

add member variables Left_hand_weapon, Right_hand_weapon as dictionaries having Name and strength as properties/keys

Example variable:left_hand_weapon = {name: "katana", strength: 20}Step2/2

add methods to the class

Explanation:

replace_weapon - will replace right or left hand weapon based on whose strength is lesser or if given weapon strength is lesser than both no replacement will be done

list_weapons - prints the list of all the weapons available till now

click here for output image

What I have tried:

Honestly, i tried this little thing but i dunno how to complete it
class character:
    Left_hand_weapon = {
        'weapon': 'katana',
        'strength': 10
    }  
    while True:
     weapon = input("Please enter the name of the weapon you found (enter Quit to quit): ")
     strength = input("Please enter the strength of the weapon you found (enter Quit to quit): ")
     if (weapon or strength) in Left_hand_weapon.values():
       print(f'Left: {weapon} has strength {strength}')
     else:
      print('That value is not in the dictionary.')
Posted
Updated 1-Nov-22 10:27am
v4
Comments
Richard MacCutchan 1-Nov-22 17:14pm    
Please do not remove the content of your question after it has received an answer.

1 solution

You need to create a weapon class with the two attributes. Also you need to add the two mentioned methods of the character class: Replace_weapon(w) and List_weapons(). As far as I can see that is all that is required. Once you have that you can create a character object, add some weapons and list them, something like:
Python
fighter = character() # create an object of the class
axe = weapon("axe", 20)
fighter.Replace_weapon(axe) # the replace method needs to replace the weakest existing weapon
fighter.List_weapons()

See also 9. Classes — Python 3.11.0 documentation[^].
 
Share this answer
 
v2

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