如果没有输入,raw_input不是None

时间:2017-07-02 09:42:39

标签: python list

编程输入默认值

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:['']

3 个答案:

答案 0 :(得分:1)

如果您只按回车键,则raw_input为空字符串,而不是无。并且您不需要默认参数上的两颗星。

答案 1 :(得分: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)

答案 2 :(得分: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