Chris Cartwright
Chris Cartwright

Reputation: 13

My code is running without calling variable in Python?

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()

Upvotes: 0

Views: 83

Answers (5)

Chris Cartwright
Chris Cartwright

Reputation: 13

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()

Upvotes: -1

Salvatore
Salvatore

Reputation: 11952

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.

Upvotes: 2

Karl Knechtel
Karl Knechtel

Reputation: 61519

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'])

Upvotes: 2

Nivardo Albuquerque
Nivardo Albuquerque

Reputation: 536

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.

Upvotes: 1

Prune
Prune

Reputation: 77837

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.

Upvotes: 1

Related Questions