Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8397195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:43:41+00:00 2026-06-09T20:43:41+00:00

How can I get Python 3 to pick a random item from a list

  • 0

How can I get Python 3 to pick a random item from a list every time I call it?
I’ve got two functions that I need to do this with. I’ve tried item = random.randint() and item = random.choice(), but those only randomize it once then store it into item.

Here are the two functions.
With function 1, I need the item to randomize every time I call it, so the player will get a random item each time. For function 2, I need the the player and rats attacks to randomize within the numbers I’ve chosen. So whenever the player attacks, it’s between 10 and 30, and when the rat attacks it’s between 10 and 15. It has to do this each turn.

Is this possible?

Function 1.

def chest(sector):
    item = random.choice(items)
    print("You see a chest, you unlock it and inside is '{0}'".format(item))
    print()
    if item in inventory:
        print("You already have a {0}".format(item))
    item_take = input("Do you wish to take the '{0}'?: ".format(item)).lower()
    if item_take == ("yes"):
        inventory.append(item)
        if item == "Armor":
            player["hp"] = 150
        print("The {0} has been added to your inventory!".format(item))
        sector()
    else:
        print("You don't take the '{0}'!".format(item))
        print()
        sector()

Function 2.

player = dict(
    name = " ",
    att = random.randint(10, 30),
    hp = 100,)

rat = dict(
    name = "Rat",
    att = random.randint(10, 15),
    hp = 20,)


def attack(player, enemy):
    firstAtt = random.randint(1, 2)#Player = 1, Enemy = 2. Checks to see who goes first.
    if firstAtt == 1:
        while player["hp"] > 0 and enemy["hp"] > 0:
            enemy["hp"] = enemy["hp"] - player["att"]
            print("You have dealt {0} damage to the {1}!".format(player["att"], enemy["name"]))
            if enemy["hp"] <= 0:
                print()
                print("You have killed the {0}".format(enemy["name"]))
                print("You have {0}HP left.".format(player["hp"]))
                break

            player["hp"] = player["hp"] - enemy["att"]
            print("The {0} has dealt {1} damage to you!".format(enemy["name"], enemy["att"]))
            if player["hp"] <= 0:
                print()
                print("The {0} has killed you!".format(enemy["name"]))
                break

    elif firstAtt == 2:
        while player["hp"] > 0 and enemy["hp"] > 0:
            player["hp"] = player["hp"] - enemy["att"]
            print("The {0} has dealt {1} damage to you!".format(enemy["name"], enemy["att"]))
            if player["hp"] <= 0:
                print()
                print("The {0} has killed you!".format(enemy["name"]))
                break

            enemy["hp"] = enemy["hp"] - player["att"]
            print("You have dealt {0} damage to the {1}".format(player["att"], enemy["name"]))
            if enemy["hp"] <= 0:
                print()
                print("You have killed the {0}".format(enemy["name"]))
                print("You have {0}HP left.".format(player["hp"]))
                break
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T20:43:42+00:00Added an answer on June 9, 2026 at 8:43 pm

    chest() is fine.

    For attack(), turn the att element into a function and then call it as appropriate.

      att=lambda: random.randint(...),
    
     ...
    
          dmg = player['att']()
           ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Python I can use get method to get value from an dictionary without
Using python and wsgiref.handlers, I can get a single variable from a form with
I can't get python-mode working in Emacs on Mac OS X (I am a
Is there any way I can get my Python code syntax highlighted in Microsoft
How can I get a transparent cursor using Python GTK3 and pyGObject. I have
After I installed MySQLdb by sudo apt-get install python-mysqldb I can import and use
is there a python library for supporting OpenType features? where can i get it?
Are there Python 3 bindings for Clutter? If so, how can I get them
In Python, how can I simply do the equivalent of dictionary.get(key, default) for lists
I am fluent in C++, Java, and Python and can pretty much pick up

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.