2017-07-02 62 views
-1

计划输入默认值是的raw_input不无当没有输入

default = [1,2,3,4] # Passing default list 
L = raw_input("Enter a list of numbers separated by comma:") 
m = L.split(",") # Spliing the list 
if raw_input == None: # If someone doesn't enter ANY INPUT 
    raw_input = default 
print default # Trying to print default input 
else: 
    print list(m) 

输出:

Enter a list of numbers separated by comma : [PRESS ENTER] 
Result:[''] 
+0

刚才你的问题是什么? –

回答

0
的raw_input

是一个空字符串,如果你只是按回车键,不无。你不需要默认参数上的两颗星。

+0

感谢您关注这个保罗。请帮助我更好地理解这一点,我完全明白你的想法。 –

+0

默认= [1,2,3,4] L = raw_input(“输入用逗号分隔的数字列表:”) m = L.split(“,”) if L =='': \t打印列表(默认) else: \t打印列表(m) –

0

这是我的方法。你有一些语法错误和逻辑错误。 而且我使用python 3,所以你可能想摆脱在打印“()”语句

default = [1,2,3,4] 
L=raw_input("Enter a list of numbers sepearted by comma:") 
m=L.split(",") #Spliing the list 
if m[0] == "": #If someone dont enter ANY INPUT 
    print (default) #Trying to print default input 
else: 
print m 

希望它有助于

这里是回答您的评论

def just_a_function(): 
    default = [1,2,3,4] #Passing default list 
    L=raw_input("Enter a list of numbers sepearted by comma:") 
    m=L.split(",") #Spliing the list 
    if m[0] == "": #If someone dont enter ANY INPUT 
    return default #Trying to print default input 
    else: 
    return m 

x=just_a_function() 
print(x) 
+0

从我所知道的(不是Python中的专家:D)你需要在函数内部返回一些东西。返回一个列表: **我编辑了我的回答你的问题** –

+0

所以你下面说的意思是,如果任何函数没有传递任何参数,如在这种情况下def just_a_function():是空的,所以你的这样做。如果我们定义了just_a_function(x):#x = just_a_function() #print(x) –

+0

这是干什么的? –

0
default = [1,2,3,4] #Passing default list 
L = input("Enter a list of numbers separated by commas:") 
L.split(',') if L else default 
相关问题