-2

I have a simple question: I have this code:

# open an HTML file on my own (Windows) computer
url = "$user data here"
webbrowser.open(url,new=new)

I need to have it so that when it is run the user can enter lets say 3 different URL's and have that code created two more times filling in the variable spot with one of the three variables in each part.

I would assume I need a loop but how can I define the variables dynamicly that way if they enter 3 or 5 urls it would generate the code to do that?

Thanks in advance, Justin

Justin
  • 111
  • 1
  • 1
  • 8

4 Answers4

1

Create an empty list. Use the list.append method to keep adding to this list whenever you need too, thereby achieving the effect you need without actually using dynamic variables.

Florin Stingaciu
  • 8,085
  • 2
  • 24
  • 45
1

If the user enters the urls, separated by a space, like this:

urls = "http://stackoverflow.com http://google.com"

then you can call webbrowser.open in a loop once for each url like this:

for url in urls.split():
    webbrowser.open(url,new=new)
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
1

do you mean you want to grab urls like:

urls = raw_input("Enter yer urls! ")

?

if so, you could just split the result and open windows for each url with something like:

for url in urls.split():
    webbrowser.open(url,new=new)
Andbdrew
  • 11,788
  • 4
  • 33
  • 37
  • This is exactly what I needed now what about if I need a variable to go between the url ie www.website.com/story/? I have done this before and remember needing some code around the variable the cleaned it up something like . var . or something like that. – Justin Nov 02 '12 at 19:07
0

Create a list with no values and use the append method in the list to dynamically assign the values to the list which you can use for iterate.

Use comma to seperate between various urls

input = raw_input("Enter  urls separted by commas ")
urls=input.split(",")

For iterate over varibles use "For" loop

for value in urls:
   webbrowser.open(value,new=new)

I hope this solves ur issue of having dynamically giving input for a function

Jon B
  • 51,025
  • 31
  • 133
  • 161
user695580
  • 41
  • 2