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 7786541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:26:01+00:00 2026-06-01T20:26:01+00:00

If I have this: def oneFunction(lists): category=random.choice(list(lists.keys())) word=random.choice(lists[category]) def anotherFunction(): for letter in word:

  • 0

If I have this:

def oneFunction(lists):
    category=random.choice(list(lists.keys()))
    word=random.choice(lists[category])

def anotherFunction():
    for letter in word:              #problem is here
        print("_",end=" ")

I have previously defined lists, so oneFunction(lists) works perfectly.

My problem is calling word in line 6. I have tried to define word outside the first function with the same word=random.choice(lists[category]) definition, but that makes word always the same, even if I call oneFunction(lists).

I want to have a different word every time I call the first function and then the second.

Can I do this without defining that word outside the oneFunction(lists)?

  • 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-01T20:26:02+00:00Added an answer on June 1, 2026 at 8:26 pm

    One approach would be to make oneFunction return the word so that you can use oneFunction instead of word in anotherFunction :

    def oneFunction(lists):
        category = random.choice(list(lists.keys()))
        return random.choice(lists[category])
    
        
    def anotherFunction():
        for letter in oneFunction(lists):              
            print("_", end=" ")
    

    Another approach is making anotherFunction accept word as a parameter which you can pass from the result of calling oneFunction:

    def anotherFunction(words):
        for letter in words:              
            print("_", end=" ")
    anotherFunction(oneFunction(lists))
    

    And finally, you could define both of your functions in a class, and make word a member:

    class Spam:
        def oneFunction(self, lists):
            category=random.choice(list(lists.keys()))
            self.word=random.choice(lists[category])
    
        def anotherFunction(self):
            for letter in self.word:              
                print("_", end=" ")
    

    Once you make a class, you have to instantiate an instance and access the member functions:

    s = Spam()
    s.oneFunction(lists)
    s.anotherFunction()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this : def steps(ns): return [random.choice([[0, 1], [1, 0], [0, -1], [-1,
I have this code: def GetSteamAccts(): #Get list of steam logins on this computer.
I have this code: def setVelocity (x, y, yaw) setVelocity (Command2d.new(x,y,yaw)) end def setVelocity
I have this code: def display(self): print self.doc.toprettyxml(indent= ) strigName ='/Users/my_user/Desktop/python/' + str(datetime.datetime.now()) +
I am writing a script and in my script I have this function: def
I have this test case def setUp(self): self.user = User.objects.create(username=tauri, password='gaul') def test_loginin_student_control_panel(self): c
I have this simple helper: def printe_result(data) a_var = ('<a href=http://'+data.a_value+'>A</a>' unless (data.a_value.nil? ||
If I have this: class A: def callFunction(self, obj): obj.otherFunction() class B: def callFunction(self,
I'm using magnolia and I have this variable: ${def.parameters.subTemplate} I want to set the
I have this function in Python: def Rotate_Vector(vector, axis, direction): where vector is a

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.