#๐Ÿ”’ Check my code

52 messages ยท Page 1 of 1 (latest)

hushed hull
#

review

livid cargoBOT
#

@hushed hull

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

hushed hull
#

yo grice

#

!paste

livid cargoBOT
#
Pasting large amounts of code

If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/

After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.

slim lodge
#

Where's the code

slim lodge
#

Why do u need 4 while loops

uncut badge
#

Well, ๐Ÿง, it's code alright

slim lodge
#

And when does ur condition become false

hushed hull
slim lodge
#

it's like an infinite loop

hushed hull
#

when i return i exit the loop

slim lodge
#

Why do u need the while loop entirely is my first question

uncut badge
#

If you don't type 1 to attack you'll just be prompted to do it again. But I imagine you have other actions you want to add later.

hushed hull
#

but

#

is the code otherwise alright? i know there are some things which can be improved but otherwise ok?

slim lodge
#

yeah it's alright ,

uncut badge
#

It's serviceable, but it can be improved.

The hard question: how can you turn these 5 functions into one function and retain the same functionality?

hushed hull
#

I have tried and only got confused

uncut badge
#

It's a tough problem we face often, but you should see here that there are common patterns between the functions. Specific bits that each function does that could just be arguments or config values, but otherwise they all do the same thing.

slim lodge
#

u can increment the value of damage by how much u want each time a certain condition is met , all within a single function

hushed hull
hushed hull
#

yk with functions its much easier

slim lodge
#

and use ur functions only for the functionality part , don't stuff all ure loops and conditional inside a function unless it's a necessary thing

hushed hull
#

ok

slim lodge
#

Thatswhat is making the code super long

hushed hull
#

ok i will try remaking it onto 2 functions or 1

#

I did watch that video saying do not try to write perfect code but yeah i wonder if after combining it all in one function it can be called perfect code and probably not

gilded marsh
#

adding attributes to your models would help a single function be able to do this:

levels_enemy = {1:{'name':enemy_name,'hp':50, "max_damage": 5, "min_damage": 5}
,2:{'name':enemy_name,'hp':60, "min_damage": 0, "max_damage": 15},
3:{'name':enemy_name,'hp':60, "min_damage": 5, "max_damage": 20},
4:{'name':enemy_name,'hp':75, "min_damage": 8, "max_damage": 25},
5:{'name':enemy_name,'hp':100, "min_damage": 10, "max_damage": 30}}

Then you pass whichever enemy, for example, is in the fight. And draws on those stats to perform the fight.e

hushed hull
#

um

#

i dont wanna do min max

#

i will just add like random from 0 to 15

gilded marsh
#

But each loop of the while loop, that is what you are doing.

#

You are getting a random integer between that max and min, so to speak.

hushed hull
#

oh

#

and about shortening the code

#
import random
player_name = input("Enter your name: ")
enemy_name = "Vindicator"
levels_player = {1:{'name':player_name,'hp':100},
2:{'name':player_name,'hp':125}
,3:{'name':player_name,'hp':150}}
levels_enemy = {1:{'name':enemy_name,'hp':50}
,2:{'name':enemy_name,'hp':70},
3:{'name':enemy_name,'hp':80}}

def main():
    attack_choice = input("Enter '1' to attack: ")
    if attack_choice == '1':
        attack_damage = 10
        levels_enemy[1]['hp'] -= attack_damage
        goblin_damage = 5
        levels_player[1]['hp'] -= goblin_damage
main()

?

#

i just keep adding if statements each with diff level right

gilded marsh
#

Shortening the code would rely on a single function that takes arguments to perform a fight.

#

This is essentially how you would make it a single function:

import random

player_name = input("Enter your name: ")
enemy_name = "goblin"

levels = {
    1: {'name': player_name, 'hp': 100, "damage": 7},
    2: {'name': player_name, 'hp': 125, "damage": 8},
    3: {'name': player_name, 'hp': 130, "damage": 10},
    4: {'name': player_name, 'hp': 150, "damage": 13},
    5: {'name': player_name, 'hp': 160, "damage": 15}
}

levels_enemy = {
    1: {'name':enemy_name, 'hp': 50, "min_damage": 5, "max_damage": 5},
    2: {'name':enemy_name, 'hp': 60, "min_damage": 0, "max_damage": 15},
    3: {'name':enemy_name, 'hp': 60, "min_damage": 5, "max_damage": 20},
    4: {'name':enemy_name, 'hp': 75, "min_damage": 8, "max_damage": 25},
    5: {'name':enemy_name, 'hp': 100, "min_damage": 10, "max_damage": 30}
}

def fight(level: int, player: dict, enemy: dict):
    while True:
        attack_choice = input("Enter '1' to attack: ")
        if attack_choice == '1':
            enemy["hp"] -= player["damage"]
            player["hp"] -= random.randint(enemy["min_damage"], enemy["max_damage"])
            print(enemy["hp"])
            print(player["hp"])
            if enemy["hp"] < 0:
                print(f"You have defeated {enemy['name']} successfully ")
                if level != 5:
                    print(f"You have passed level {level} and now you can now move onto level {level + 1}! ")                    
                break

                
for i in range(1,6):
    fight(i, levels[i], levels_enemy[i])
print("Congrats! You have completed the game! ")
#

I have done what others described to you. Take what is common between each function, and essentially condense it down.
The function needs to know who is fighting what, and what their stats are. But each fight is the same, only the enemy and the player models change along with their stats.

#

There are still some oddities in your code choice (but perhaps you intend to add more, as someone else mentioned) but this is exactly the logic you had, condensend into a single fight() mechanism.
Please ask questions if you have any - we do not generally like to just provide code, and that's not what we are about.

hushed hull
#

k

#

@gilded marsh

#

!close

livid cargoBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.