Python Forum
Can someone please help me with my code? (sorry to ask for so much help)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can someone please help me with my code? (sorry to ask for so much help)
#1
So, I'm trying to make a code to dodge the monsters attack, I want to pick a random value from dodge, and then depending on the value that is picked, I want it to print whether or not you managed to dodge the attack.


player = {'name': 'matthew', 'attack': 10, 'heal': 25, 'health': 100}
monster = {'name': 'Joe', 'attack': 25, 'health': 100}
health_list = ['Health', 'health']
game_running = True
dodge = [1, 2]
import random
dodge1 = dodge

while game_running == True:

    player_won = False
    monster_won = False

    print('\n-------------------------------------')
    print('please select an action')
    print("Type ? for a list of commands")
    print('-------------------------------------')
    print('\n1) Attack')
    print('2) Heal')
    print('3) Dodge')
    print('\n-------------------------------------')

    player_choice = input('>>>')

    if player_choice == '1':
        print('\nYou attacked the monster!')
        monster['health'] = monster['health'] - player['attack']
        if monster['health'] <= 0:
          player_won = True

        else:
          player['health'] = player['health'] - monster['attack']
          if player['health'] <= 0:
            monster_won = True

        print('Player health: ', player['health'])
        print('Monster health: ', monster['health'])

    elif player_choice == '2':
        print('\n---------------------------------')
        print('You used a potion and healed 25 HP! ')
        print('---------------------------------')
        player['health'] = player['health'] + player['heal']
    elif player_choice in health_list:
      print('\n-----------------------------------')
      print('your health is: ', player['health'])
      print("Monster's health is:", monster['health'] )
      print('-----------------------------------')
    elif player_choice == '?':
      print('\n-----------------------------------')
      print('Heres a list of commands:')
      print('-----------------------------------')
      print('- Health: shows your current health and the enemies current health.')
      print('- Version: shows the games current version')
      print('-----------------------------------')
    elif player_choice == '3':
      print(random.choice(dodge1))
      if dodge == 1:
        print("You dodged the monsters attack!")
      elif dodge == 2:
        print("Your dodge attempt failed!")
      else:
          print("null")
    else:
      print('\n-----------------------------------')
      print('Invalid Input: use 1, 2, or 3')
      print('-----------------------------------')
    
    if player_won == True or monster_won == True:
        game_running = False
Reply
#2
You need to assign the result of random.choice, and then test that. Something like:

elif player_choice == '3':
    attempt = random.choice(dodge1)
    if attempt == 1:
        print('You dodged the monsters attack!')
    ...
There's a little trick for random true/false: random.randrange(2) returns 0 or 1 randomly. 0 evaluates to False in a conditional, and 1 evaluates as true. So you can do this:

elif player_choice == '3':
    dodged = random.randrange(2)
    if dodged:
        print("You dodged the monster's attack!")
    ...
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
thank you very much! Smile
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020