'Making a Naruto Game in Python
i have been stuck in this problem for a while now,
am trying to make a game where the user is given only 100 number to create a well defined shinobi ( got the name from watch Naruto and i love the series)
so i made my own class attributes. am trying to create a function to store the game inside and trying to make it so if the player uses more than 100 or = 100, the code should stop running but instead the code run even to negative a figure
let me put out the code for understanding ( sorry for my long write up, its actually my first time here)
print("ok, lets beging the game")
import Avatars # importing my own customised class
s = Avatars.Shinobi('naruto', 'uzimaki', 'genin', 100, 50, 70) # original class attributes
name = input('enter your name: ')
print(f'welcome onboard {name}, so you have to make your own ninja using (1- 100) '
f'and each time you make a ninja, we minus the number from the 100 you have '
f'GoodLuck!!')
print(" all you have to do is make a ninja that has 'strength', 'chakra', and 'skill' ")
def game(): # putting everything inside a function
try:
numb = 100 # the max number given by me to the player
while numb > 0:
a = (s.set_skill()) # using the imported class
a1 = int(a)
numb -= a
print('you have ', numb)
b = (s.set_chakra()) # using the imported class
b1 = int(b)
numb -= b
print(' you have ', numb)
c = (s.set_strength()) # using the imported class
c1 = int(c)
numb -= c
print('you have', numb)
if numb < 0: # using an if statement to cut the game half way, if the user inputs more than 100
print('you have run out of numbers')
else:
print('you still have more numbers to go!!')
except:
print('enter only number')
print(game())
OUTPUT:
ok, lets beging the game
enter your name: emma
welcome onboard emma, so you have to make your own ninja using (1- 100) and each time you make a ninja, we minus the number from the 100 you have GoodLuck!!
all you have to do is make a ninja that has 'strength', 'chakra', and 'skill'
enter skill rate (1 - 100): 50
you have 50
enter chakra level (1 - 100): 50
you have 0
enter strength level (1 - 100): 60
you have -60
you have run out of numbers
the code was suppose to stop, since i have input 50 twice and have no number left
Thanks for the help!
Solution 1:[1]
Hi Emmanuel Chidera, this is happening because you are substracting from numb after each set operation but checking numb value 'til the end. In order to prevent this to occur, you may check the numb variable value after each set operation and if its value is <= 0 you end right there the program execution.
To help you to understand this situation a bit better, I'm going to provide this short code snippet demonstrating the difference between your approach (checking numb value at the end) and the possible solution.
def initShinobi():
#This is similar to your approach
print("Inside initShinobi function\n")
maxPoints = 100 # the maximum stat points
skill = int(input("Enter skill level: "))
maxPoints -= skill
print(f'You have {maxPoints} points left to assign.')
chakra = int(input("Enter chakra level: "))
maxPoints -= chakra
print(f'You have {maxPoints} points left to assign.')
strength = int(input("Enter strength level: "))
maxPoints -= strength
print(f'You have {maxPoints} points left to assign.')
if maxPoints < 0:
print('you have run out of numbers')
def haveAvailablePoints(points):
"""Helper function to check your available points"""
if points > 0:
print(f'You have {points} points left to assign.')
return True
else:
print('You have run out of points')
return False
def initShinobi2():
# This is the possible solution
print("\n\nInside initShinobi2 function\n")
maxPoints = 100 # the maximum stat points
skill = int(input("Enter skill level: "))
maxPoints -= skill
if not haveAvailablePoints(maxPoints):
return
chakra = int(input("Enter chakra level: "))
maxPoints -= chakra
if not haveAvailablePoints(maxPoints):
return
strength = int(input("Enter strength level: "))
maxPoints -= strength
if not haveAvailablePoints(maxPoints):
return
if __name__ == '__main__':
initShinobi()
initShinobi2()
Assuming skill = 50, chakra = 50 and strength = 60 the code snippet above produces this output:
Inside initShinobi function
Enter skill level: 50
You have 50 points left to assign.
Enter chakra level: 50
You have 0 points left to assign.
Enter strength level: 60
You have -60 points left to assign.
you have run out of numbers
Inside initShinobi2 function
Enter skill level: 50
You have 50 points left to assign.
Enter chakra level: 50
You have run out of points
Successful execution. Press Enter to exit...
As you can see, in initShinobi2 function after I entered skill and chakra level it won't ask for me to enter the strength level since we have reached the maximum value with the first two parameters.
Note: You may notice in initShinobi2 function that if you type skill = 50 and chakra = 60 the output will be similar to the demonstration here but logically it has overpassed the maximum value, but that's something that I delegate you to solve.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
