2012-01-15 187 views
0

我有一个变量:在一个循环变化的变量

user = "jobrr" 

我要循环以下,但改变什么变量每个循环周期(所以在循环结束后表示预定义什么为即将到来的循环变量将)

example loop as long as y = true 
    openurl = urllib.urlopen("https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&contributor_details&include_rts=true&screen_name="+user+"&count=3600") 
    user = user + "a" #just for example 

我该怎么做?

回答

6
user = 'username' 
y = True 

while y: 
    openurl = urllib.urlopen("https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&contributor_details&include_rts=true&screen_name="+user+"&count=3600") 
    user += "a" 
    #You have to do something in here to change y or this will be an infinite loop 
1
user = 'username' 
y = True 

while y: 
    openurl = urllib.urlopen("https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&contributor_details&include_rts=true&screen_name="+user+"&count=3600") 
    user = function_that_returns_user_name() 

你只需要定义function_that_returns_user_name()

+0

我觉得这种方法对于长期发展更可持续(我怀疑你会得到,只是“一个的追加到它的名字真正的比赛)。 – Makoto 2012-01-15 17:26:28

+0

@Makoto,我同意,这就是为什么我写了我的答案。但OP有选择。 :-) – joaquin 2012-01-15 18:32:31