Punith Kothari
Punith Kothari

Reputation: 45

raw_input is not None when there's no input

Program to input default value

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)

Output:

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

Upvotes: 0

Views: 443

Answers (3)

Transhuman
Transhuman

Reputation: 3547

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

Upvotes: 0

Nimesha Kalinga
Nimesha Kalinga

Reputation: 261

Here is my approach. You have some syntax errors and logical errors. And i'm using python 3, so you may want to get rid of the "()" in the print statements

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

hope it helps

Here is the answer to your comment

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)

Upvotes: 0

Paul Collingwood
Paul Collingwood

Reputation: 9116

raw_input is an empty string if you just press return, not None. And you don't need the two stars on the default argument.

Upvotes: 1

Related Questions