Skip to main content
Code Review

Return to Answer

added 2 characters in body
Source Link
depperm
  • 1.1k
  • 6
  • 14
import time
health= 100
play = True
def print_health(hp):
 l='しかく'
 s='|'
 for i in range(10):
 if i<hp/10:
 s+=l # show a bar of health if multiple of 10
 else:
 s+=" " # else fill the missing hp with space
 if i%2==1:
 s+='|' # print a | every 2 characters
 print(s)
while play:
 try:
 damage= int(input("How much damage would you like to take?" ))
 except ValueError:
 print("That isn't a command")
 continue # starts the loop over
 remaining_health = health - damage
 print("You took {0} damage!".format(damage))
 health=remaining_health
 print(" --------------")
 print_health(remaining_health)
 print(" --------------")
 
 if remaining_health <= 0:
 print("You died!")
 break # exit the loop
 
 again = 'x'
 while again not in 'YN': # you may want to cast againt to upper
 again = input("Would you like to play again? |Y/N| ")
 if again == "N":
 PAplay = False
 elif again != 'Y':
 print("That is not a command...")
print("The program will now close")
time.sleep(5)
import time
health= 100
play = True
def print_health(hp):
 l='しかく'
 s='|'
 for i in range(10):
 if i<hp/10:
 s+=l # show a bar of health if multiple of 10
 else:
 s+=" " # else fill the missing hp with space
 if i%2==1:
 s+='|' # print a | every 2 characters
 print(s)
while play:
 try:
 damage= int(input("How much damage would you like to take?" ))
 except:
 print("That isn't a command")
 continue # starts the loop over
 remaining_health = health - damage
 print("You took {0} damage!".format(damage))
 health=remaining_health
 print(" --------------")
 print_health(remaining_health)
 print(" --------------")
 
 if remaining_health <= 0:
 print("You died!")
 break # exit the loop
 
 again = 'x'
 while again not in 'YN': # you may want to cast againt to upper
 again = input("Would you like to play again? |Y/N| ")
 if again == "N":
 PA = False
 elif again != 'Y':
 print("That is not a command...")
print("The program will now close")
time.sleep(5)
import time
health= 100
play = True
def print_health(hp):
 l='しかく'
 s='|'
 for i in range(10):
 if i<hp/10:
 s+=l # show a bar of health if multiple of 10
 else:
 s+=" " # else fill the missing hp with space
 if i%2==1:
 s+='|' # print a | every 2 characters
 print(s)
while play:
 try:
 damage= int(input("How much damage would you like to take?" ))
 except ValueError:
 print("That isn't a command")
 continue # starts the loop over
 remaining_health = health - damage
 print("You took {0} damage!".format(damage))
 health=remaining_health
 print(" --------------")
 print_health(remaining_health)
 print(" --------------")
 
 if remaining_health <= 0:
 print("You died!")
 break # exit the loop
 
 again = 'x'
 while again not in 'YN': # you may want to cast againt to upper
 again = input("Would you like to play again? |Y/N| ")
 if again == "N":
 play = False
 elif again != 'Y':
 print("That is not a command...")
print("The program will now close")
time.sleep(5)
deleted 10 characters in body
Source Link
depperm
  • 1.1k
  • 6
  • 14
import time
health= 100
play = True
def print_health(hp):
 l='しかく'
 s='|'
 for i in range(10):
 if i<hp/10:
 s+=l # show a bar of health if multiple of 10
 else:
 s+=" " # else fill the missing hp with space
 if i%2==1:
 s+='|' # print a | every 2 characters
 print(s)
while play == (True):
 try:
 damage= int(input("How much damage would you like to take?" ))
 except:
 print("That isn't a command")
 continue # starts the loop over
 remaining_health = health - damage
 print("You took {0} damage!".format(damage))
 health=remaining_health
 print(" --------------")
 print_health(remaining_health)
 print(" --------------")
 
 if remaining_health <= 0:
 print("You died!")
 break # exit the loop
 
 again = 'x'
 while again not in 'YN': # you may want to cast againt to upper
 again = input("Would you like to play again? |Y/N| ")
 if again == "N":
 PA = False
 elif again != 'Y':
 print("That is not a command...")
print("The program will now close")
time.sleep(5)
import time
health= 100
play = True
def print_health(hp):
 l='しかく'
 s='|'
 for i in range(10):
 if i<hp/10:
 s+=l # show a bar of health if multiple of 10
 else:
 s+=" " # else fill the missing hp with space
 if i%2==1:
 s+='|' # print a | every 2 characters
 print(s)
while play == (True):
 try:
 damage= int(input("How much damage would you like to take?" ))
 except:
 print("That isn't a command")
 continue # starts the loop over
 remaining_health = health - damage
 print("You took {0} damage!".format(damage))
 health=remaining_health
 print(" --------------")
 print_health(remaining_health)
 print(" --------------")
 
 if remaining_health <= 0:
 print("You died!")
 break # exit the loop
 
 again = 'x'
 while again not in 'YN': # you may want to cast againt to upper
 again = input("Would you like to play again? |Y/N| ")
 if again == "N":
 PA = False
 elif again != 'Y':
 print("That is not a command...")
print("The program will now close")
time.sleep(5)
import time
health= 100
play = True
def print_health(hp):
 l='しかく'
 s='|'
 for i in range(10):
 if i<hp/10:
 s+=l # show a bar of health if multiple of 10
 else:
 s+=" " # else fill the missing hp with space
 if i%2==1:
 s+='|' # print a | every 2 characters
 print(s)
while play:
 try:
 damage= int(input("How much damage would you like to take?" ))
 except:
 print("That isn't a command")
 continue # starts the loop over
 remaining_health = health - damage
 print("You took {0} damage!".format(damage))
 health=remaining_health
 print(" --------------")
 print_health(remaining_health)
 print(" --------------")
 
 if remaining_health <= 0:
 print("You died!")
 break # exit the loop
 
 again = 'x'
 while again not in 'YN': # you may want to cast againt to upper
 again = input("Would you like to play again? |Y/N| ")
 if again == "N":
 PA = False
 elif again != 'Y':
 print("That is not a command...")
print("The program will now close")
time.sleep(5)
Source Link
depperm
  • 1.1k
  • 6
  • 14

I'll echo @TopinFrassi to have better-named variables.

Since you don't mention a limit on the amount of damage you can take. I believe you need to change your prompt to be something like (and then you'll want to check that they did):

damage = input("How much damage would you like to take?(interval of 20)" )

Or change your logic after your current prompt. First, you should try casting the damage right away and surround it with a try...except in case they enter a string. Then have a single line to print how much damage was done, even if you stick to multiples of 20. I would make a print_health function that handles the health bar so that you can get even more precise. If your health is less than or equal to 0 you should stop the game.

While asking to play again it should probably be in a loop in case they enter something invalid. And then I'd move the sleep and print outside of the loop.

import time
health= 100
play = True
def print_health(hp):
 l='しかく'
 s='|'
 for i in range(10):
 if i<hp/10:
 s+=l # show a bar of health if multiple of 10
 else:
 s+=" " # else fill the missing hp with space
 if i%2==1:
 s+='|' # print a | every 2 characters
 print(s)
while play == (True):
 try:
 damage= int(input("How much damage would you like to take?" ))
 except:
 print("That isn't a command")
 continue # starts the loop over
 remaining_health = health - damage
 print("You took {0} damage!".format(damage))
 health=remaining_health
 print(" --------------")
 print_health(remaining_health)
 print(" --------------")
 
 if remaining_health <= 0:
 print("You died!")
 break # exit the loop
 
 again = 'x'
 while again not in 'YN': # you may want to cast againt to upper
 again = input("Would you like to play again? |Y/N| ")
 if again == "N":
 PA = False
 elif again != 'Y':
 print("That is not a command...")
print("The program will now close")
time.sleep(5)
lang-py

AltStyle によって変換されたページ (->オリジナル) /