2012-01-13 62 views
2

我一直在尝试一段时间才能访问我最近返回的值,并在if语句中使用它,而不必调用该值。访问以前返回的值 - Python3.2

基本上我有一个while循环调用一个函数,允许用户输入,然后将输入返回到循环中。

while selection() != 0: ## Calls the "WHAT WOULD YOU LIKE TO DO" list and if it is 0 quits the script 
    input() ## just so it doesn't go straight away 
    if selection.return == 1: ## This is what I would like to happen but not sure how to do it... I've googled around a bit and checked python docs 

见,如果我把:

if selection() == 1: 

它会工作,但再次显示“你会喜欢做的事”的文字...

对不起,如果这是一个明显的解决方案,但帮助将非常感谢:)

+2

你必须使用一个变量。所以,虽然真:sel = selection();如果sel == 0:break;否则:#做任何你做的事 – AdamKG 2012-01-13 19:41:05

回答

8

这就是为什么你会将结果存储在一个变量,所以你可以在将来参考它。喜欢的东西:

sel = selection() 
while sel != 0: 
    input() 
    if sel==1: 
     ... 
    sel = selection() 
+0

哦,这使得很多感觉哈哈!谢了哥们! – Clement 2012-01-13 19:51:41

3

这只是张贴的答案(实在是太尴尬加入了注释)的选择,但请不要改变你的答案:)无论你是否喜欢它更好有点可以选择偏好,但我喜欢不必重复输入源代码行,尽管它会“迷惑”环路条件:

while True: 
    sel = selection() 
    if sel == 0: # or perhaps "if not sel" 
     break 
    input() 
    if sel == 1: 
     ... 

快乐编码。