Hey!

Sorry if the title looks a little odd and confusing.

Basically what I want it to in my code, have Python input something as if it were the user.

Does anyone know how to do this?

Thanks
Andrew!

You mean something like this:

# your Python3 program ...

# for Python2 use raw_input
input("Press enter to go on ... ")

# this would be Python's input
python = "Python is great!"

print(python)

Can you be a bit more specific, please? A few things we'd need to know include:

  • The version of Python you're using, and the platform it is running on.
  • Your goal - is this to test the program, or spoof it in some way? Did you want this to be in the same program, or between two different programs?
  • Is this a console program, or a GUI program? If a console program, do you need to have 'raw' input, or is standard, buffered input enough? If for a GUI program, what toolkit are you using?
  • What kind of input do you need to simulate? Text, mouse button-downs, mouse movements?

I might add that chances are, you don't actually need to fake the user input; usually when that kind of thing comes up, it's a sign that your coupling the input to tightly with the computation. What I mean by this is, it is usually better to separate the parts of the program that actually work on the data from the parts that read in and print out the data. By decomposing the program this way, you usually get a more robust program, and one that is easier to test and maintain as well.

Yeah sorry. No I am running Python 2.7 on Windows 7

What I am trying to achieve is:

import random

class classLocation:
    def createLocation(self,location):
        self.location=location
    def displayLocation(self):
        return self.location
    def state(self):
        print "Your Location Is %s" % self.location

first=classLocation()
second=classLocation()
first.createLocation('Woods')
second.createLocation('Plains')

choice = raw_input ("What Is Your Next Step? ")
if choice == explore:
    explore = random.randint(1, 2)
    if explore == 1:
        first.classLocation()
    elif explore == 2:
        second.classLocation()

and make the if explore == 1, I want the computer to enter first.classLocation in a raw_input as if it were me doing it, but it isn't me.

You getting me?

import random

class classLocation:
    def createLocation(self,location):
        self.location=location
    def displayLocation(self):
        return self.location
    def state(self):
        print "Your Location Is %s" % self.location

first=classLocation()
second=classLocation()
first.createLocation('Woods')
second.createLocation('Plains')

#choice = raw_input ("What Is Your Next Step? ")
choice = 'explore'  # let the computer do it
if choice == 'explore':
    explore = random.randint(1, 2)
    if explore == 1:
        first.classLocation()
    elif explore == 2:
        second.classLocation()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.