#๐ Check my code
52 messages ยท Page 1 of 1 (latest)
@hushed hull
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.
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.
Where's the code
Why do u need 4 while loops
Well, ๐ง, it's code alright
And when does ur condition become false
yeah its code
it's like an infinite loop
Why do u need the while loop entirely is my first question
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.
yeah
but
is the code otherwise alright? i know there are some things which can be improved but otherwise ok?
yeah it's alright ,
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?
I have tried and only got confused
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.
u can increment the value of damage by how much u want each time a certain condition is met , all within a single function
Yeah I tried combining but I couldnt come up with anything and I dont want to gpt
the damage the enemy damage the levels system the specific damage on each level it all gets too complicated
yk with functions its much easier
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
ok
Thatswhat is making the code super long
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
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
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.
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
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.
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.