0

When I run the below code my websites or steam still open. Shouldn't I need to state print(link) or print(steam) for them to open?

import os
import webbrowser
import subprocess
import random

urls = ['https://www.ft.com/', 
        'https://www.youtube.com/watch?v=xvqcFcfhUVQ',
        'https://roadmap.sh/backend',
        'https://www.youtube.com/watch?v=YYXdXT2l-Gg&list=PL-osiE80TeTskrapNbzXhwoFUiLCjGgY7']

foxpath = 'C:/Program Files/Mozilla Firefox/Firefox.exe %s'

link = webbrowser.get(foxpath).open(random.choice(urls))
steam = subprocess.call(['C:/Program Files (x86)/Steam/Steam.exe'])

Why does this happen?

I eventually want to run the program from a function call, like below.

def wildcard():

    print(random.choice(link, steam))

wildcard()
2
  • 4
    There is no such thing as "calling a variable". You call functions. Commented Mar 31, 2020 at 20:02
  • Whats not making sense......your code says thins like webbrowser.get.open. this does just waht it says it will open the broswer and what your storing in link is the result of opening that website. same with steam. subprocess.call will call steam and you will store the result of opening steam in a variable called steam. Commented Mar 31, 2020 at 20:03

5 Answers 5

2

No, there is nothing special about print. print is just a function that takes in some value and displays it to the user.

If you had instead steam = 3 * 4, would you be surprised to learn that the value 12 is computed, and steam becomes a name for that value, even if you don't do anything with it? It's the same thing here - calling subprocess.call causes the program to launch, and it has nothing to do with the name steam, nor anything that you do or don't do with that name subsequently.

If you were to add the print(steam) line that you have in mind, what it would display (after you close steam and control returns to your program) is the "return code" of that program - this gets into the details of how your operating system works, but most likely it would be something like 0.

If you want something that you can later call in order to launch Steam - well, that's a function. Like you already know how to do:

def steam():
    subprocess.call(['C:/Program Files (x86)/Steam/Steam.exe'])
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I understand now.
2

As soon as you issue webbrowser.get or subprocess.call, they execute. Your variables are really storing the return values of those functions, not aliases to those function calls.

If you want to alias the function calls as it appears you are intending, you could do something like this:

def open_link():
    return webbrowser.get(foxpath).open(random.choice(urls))

def open_steam():
    return subprocess.call(['C:/Program Files (x86)/Steam/Steam.exe'])

Then your top level would be:

def wildcard():
     random.choice([link, steam])()

wildcard()

Note the syntax difference for choosing the functions randomly. See this answer for more clarification.

Comments

1

You do invoke something:

steam = subprocess.call(['C:/Program Files (x86)/Steam/Steam.exe'])

The documentation for subprocess.call is clear on what the call method does: it invokes the given argument as a subprocess.

Comments

1

The problem is that your code isn't inside a function, so, when you execute it, it runs all the diretives including steam = subprocess.call(['C:/Program Files (x86)/Steam/Steam.exe']) Which calls C:/Program Files (x86)/Steam/Steam.exe, opening your steam app.

Hope it helps.

Comments

-1

Thank you, I understand now. I actually just resolved the problem with the following

def wildcard():
    for x in range(1):
        rand = random.randint(1, 10)
        if rand > 5:
            link = webbrowser.get(foxpath).open(random.choice(urls))
        else:
            subprocess.call(['C:/Program Files (x86)/Steam/Steam.exe'])


wildcard()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.