Reputation: 1
I'm pretty new to Python, and coding in general, and currently working with Python 3.5. I wanted to learn to automate things that need text boxes filled to run. I experimented with the code below in a video game just as a way to learn.
I would like it to substitute different item numbers that are in a list into the consolecommand text.
Thank you for your time.
The code:
from pynput.keyboard import Key, Controller
import time
from keyboard import press
keyboard = Controller()
def submit():
press('enter')
def Keyboardpress1():
keyboard.press('/')
keyboard.release('/')
def waitone():
time.sleep(1)
def consolecommand():
keyboard.type("giveitem 172 49000")
def deepsix():
time.sleep(6)
def main():
deepsix()
submit()
waitone()
Keyboardpress1()
waitone()
consolecommand()
waitone()
submit()
deepsix()
if __name__ == '__main__':
main()
As you can see, the code is designed to hit a number to activate the command console in game, then type a command. The pauses between typing/keyboard are because the code doesn't run smoothly without them.
Thanks, appreciate any help.
Edit: I implemented fixes I learned from suggested resource materials. Now my problems are much fewer. Thank you, people of the comments section.
Edit1: I implemented new code to fix a previous problem. Now to get the consolecommand to change numbers.
Upvotes: 0
Views: 69
Reputation: 377
@Connor Winterton, here's a very simplistic example of using a parameter to save repeating yourself. Consider func1() that performs a simple calculation and prints a hard-coded message. Consider func2() that is nearly the same except the message is different. By passing a parameter into func3() you can write just one function that will perform the task of both to reduce your code and clutter, and give you the ability to pass in any other messages you may want in the future:
def func1():
a = 1+2
print(a,'hello')
def func2():
a = 1+2
print(a,'goodbye')
def func3(my_msg):
a = 1+2
print(a,my_msg)
# call the hardcoded functions
func1()
func2()
# call the new versatile function
func3('hello')
func3('goodbye')
func3('See you later!')
Upvotes: 0