mercredi 18 septembre 2019

Storing Function into a List

i am new to programming, started around a month ago. This is also my first post on StackOverflow. I am self teaching from a text called Automate The Boring Stuff. Basically i am trying to get this game called 2048 to automatically play itself, by doing so, i used random.randint to automatically grab a function from the list "keys", however, it continuously returns "None."

I've completed the task through option 1 but decided that i could save a lot of time from the continuous typing if i did Option 2 instead.

Thanks for taking your time reading this!

option 1

from selenium import webdriver

from selenium.webdriver.common.keys import Keys

import random, time

browser = webdriver.Firefox()

browser.get('https://play2048.co')

htmlElem = browser.find_element_by_tag_name('html')

keys = [htmlElem.send_keys(Keys.DOWN), htmlElem.send_keys(Keys.UP), htmlElem.send_keys(Keys.LEFT), htmlElem.send_keys(Keys.RIGHT)]

def random2048(ranNum):

if ranNum == 1:

    return htmlElem.send_keys(Keys.DOWN)

if ranNum == 2:

    return htmlElem.send_keys(Keys.UP)

if ranNum == 3:

    return htmlElem.send_keys(Keys.LEFT)

if ranNum == 4:

    return htmlElem.send_keys(Keys.RIGHT)

while True:

time.sleep(0)

ranNum = random.randint(1, 4)

random2048(ranNum)

option 2

from selenium import webdriver

from selenium.webdriver.common.keys import Keys

import random, time

browser = webdriver.Firefox()

browser.get('https://play2048.co')

htmlElem = browser.find_element_by_tag_name('html')

keys = [htmlElem.send_keys(Keys.DOWN), htmlElem.send_keys(Keys.UP), htmlElem.send_keys(Keys.LEFT), htmlElem.send_keys(Keys.RIGHT)]

def random2048():

keys[random.randint(0, len(keys) - 1)]

while True:

random2048()

my expectations are that the output of option 2 to be exactly like option 1. Thank you for taking you time reading this!




Aucun commentaire:

Enregistrer un commentaire