-3

I'm having what will definitely be a very simple issue but I can't get my head around it.

I'm trying to get the user to input a desired website name, then use that to decide on the full name use that in another function. Here's the basics of the function.

def website():
    x = input("Enter url:")
    global url
    if 'Google' in x:
        url = ("www.google.com")
    else:
        "Try again!"
        website()

This only works if I have global url there. Otherwise, it breaks. In the main function it tries to use the output url from website() straight away, but returns:

NameError: name 'url' is not defined

If global url isn't there. The next function literally prints the result of the previous function. It will do more but as I can't even get it to print yet I haven't got to that stage.

Versace
  • 175
  • 1
  • 3
  • 11
  • 2
    you can have the `website` function *return* the `url` to the main scope using `return url`. Then on the main scope, you need to assign the call of `website` to a variable like e.g., `my_url = website()` – Ma0 Feb 05 '18 at 15:31
  • Could you expand a bit further? – Versace Feb 05 '18 at 15:32
  • @Versace What exactly have you tried, and how does it not work? – glglgl Feb 05 '18 at 15:33
  • 2
    @Versace This is *impossible*. Try again. – Ma0 Feb 05 '18 at 15:33
  • 1
    You must be making some other error -- which we can't see because your question only has the original code. Please update your question to have the new code which returns the value, and also include the main calling code. – John Gordon Feb 05 '18 at 15:34
  • Ok, I've updated it. – Versace Feb 05 '18 at 15:41
  • The updated code uses `global`, which you said works -- so this code _doesn't demonstrate the problem_. Also you didn't show the main calling code. – John Gordon Feb 05 '18 at 15:45
  • Ok, I've misunderstood you. The previous code was the problem. Exactly as it was, without global, it doesn't work and gives a NameError. The main() calling code is quite literally only currently calling website() and then doing a print url at the moment. – Versace Feb 05 '18 at 15:49

2 Answers2

0

You could pass the variables by returning the variable from a functions. Such as:

def foo():
    url="google.com"
    return url  #this will send the variable URL to wherever it was called from
def bar(url):
    print url

bar(foo())  #this calls the function `bar()` 

The function bar() accepts a variable that we called url (this does not need to be the same as the variable in function foo() but it can be). Then inside of the parenthesis we call foo() which returns the data inside of url.

Zack Tarr
  • 851
  • 1
  • 8
  • 28
0

I don't understand nor see what you have or have not tried, but I'll try my best to guess:

def website():
    x = input("Enter url:")
    if 'Google' in x:
        url = ("www.google.com")
        return url
    else:
        print("Try again!")
        return website()

if __name__ == '__main__':
    url = website()
    print(url)

But be aware that this recursive call might end you in trouble (Stack overflow) if it is called more than 1024 times (in the default implementation).

Better do

def website():
    while True:
        x = input("Enter url:")
        if 'Google' in x:
            url = ("www.google.com")
            return url
        print("Try again!")

People might find it ugly to use an endless loop, but it is superior to the recursive call.

glglgl
  • 89,107
  • 13
  • 149
  • 217