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!
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!
As ’s example shows, the real need is for the program to decide an action, not for the console prompt itself to be magically typed. The most maintainable approach is to decouple input from game logic (as suggested): make the prompt a callable parameter so the same logic can run interactively, deterministically, or under test.
import random
def take_turn(get_action):
action = get_action("Next step? ")
if action == 'explore':
return random.choice(['Forest', 'Meadow'])
return 'wait'
# automated run: simulate selecting "explore"
destination = take_turn(lambda _prompt: 'explore')
print "New location:", destination Quick test shortcuts like directly assigning the input variable (as showed) are fine for quick checks, but refactoring avoids brittle code. Two bugs visible in the thread should be highlighted: comparing to an undefined name (for example if choice == explore) must compare to the string ('explore'), and calling the class or a non-existent attribute on an instance does not “move” state. Use an explicit state reference instead — e.g. current = chosen_location then call the instance method that reports state.
If refactoring is not possible, tests can patch the console input at runtime (Python 2: patch '__builtin__.raw_input') using the backport mock package. Console-level automation can be handled this way; GUI automation or keystroke tools (pyautogui, etc.) are a different class of solution. See the Python 2 input documentation and the mock package for patching examples: raw_input — Python 2 docs, mock on PyPI.
Jump to Post— Lardmeister 461You 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)
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:
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()
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.