Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a validation to the inputs #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion DRAGOBNALL_Z/classes/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,20 @@ def choose_target(self, enemies):
if enemy.get_hp() != 0:
print(" " + str(i) + "." + enemy.name)
i += 1
choice = int(input(" choose target: ")) - 1

# Input Choose and Validation
while True:
try:
choice = int(input(" choose target: ")) - 1

except ValueError:
choice = -5

if choice >= 0 and choice <= 2:
break

print(" " + bcolors.WARNING + "Invalid option, try again\n" + bcolors.ENDC)

return choice

def get_enemy_stats(self):
Expand Down
55 changes: 44 additions & 11 deletions DRAGOBNALL_Z/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,23 @@
enemy.get_enemy_stats()

for player1 in players:
player1.choose_action()
choice = input(" Choose Action: ")
index = int(choice)-1


# Input Choose Action and Validation
while True:
player1.choose_action()

try:
index = int(input(" Choose Action: "))-1

except ValueError:
index = -1

if index >= 0 and index <= 2:
break

print(" " + bcolors.WARNING + "Invalid option, try again\n" + bcolors.ENDC)

# Choices
if index == 0:
dmg = player1.generate_damage()
enemy = player1.choose_target(enemies)
Expand All @@ -81,11 +94,21 @@

elif index == 1:
player1.choose_magic()
magic_choice = int(input(" Choose Magic:")) - 1

if magic_choice == -1:
continue
# Input Choose Magic and Validation
while True:
try:
magic_choice = int(input(" Choose Magic:")) - 1;

except ValueError:
magic_choice = -5;

if magic_choice >= 0 and magic_choice <= 5:
break

print(" " + bcolors.WARNING + "Invalid option, try again\n" + bcolors.ENDC)

# Magic nuances
spell = player1.magic[magic_choice]
magic_dmg = spell.generate_damage()

Expand Down Expand Up @@ -113,13 +136,23 @@

elif index == 2:
player1.choose_item()
item_choice = (int(input("Choose item: ")))-1

if item_choice == -1:
continue

# Input Choose Item and Validation
while True:
try:
item_choice = (int(input("Choose item: ")))-1

except ValueError:
item_choice = -5

if item_choice >= 0 and item_choice <= 5 or item_choice == -2:
break

print(" " + bcolors.WARNING + "Invalid option, try again\n" + bcolors.ENDC)

item = player1.items[item_choice]["item"]

# Items nuances
if player1.items[item_choice]["Quantity"] == 0:
print(bcolors.FAIL + "\n" + "None Left...." + bcolors.ENDC)
continue
Expand Down