1

I'm getting floating point parameters from user in a script and wondering if there is a better/effecient way including an option like if user only supplies one parameter (or no at all), is there any way that i default the remaining two as 0.0? And may be some better way to store in file

#inside a loop to keep getting values         
line = raw_input("Enter three parm values: ")
x=line.split(' ')
fa=fb=fc=0.0 

a=x[0]
b=x[1] #what if user only supplies only one?
c=x[2] # how can i leave this or default to 0.0?

fa=float(a)
fb=float(b)
fc=float(c)

file=open(inputFile, 'a+')
file.write(name)
file.write("\t")
file.write(a)
file.write(" ")
file.write(b)
file.write(" ")
file.write(c)
file.write("/n")

4 Answers 4

3

Why don't you use a list to hold an arbitrary number of variables?

floats = [float(var) if var else 0. 
          for var in raw_input("Enter three parm values: ").split(' ')]
with open(inputFile, 'a+') as f:
    f.write(name + '\t' + ' '.join(str(f) for f in floats) + '\n')

If you want to pad this list with extra zeros up to three parameters, then you could do this:

floats = [1]  # For example.
if len(floats) < 3:
    floats += [0] * (3 - len(floats))

>>> floats
[1, 0, 0]
Sign up to request clarification or add additional context in comments.

2 Comments

Best answer i believe, appreciate your clean logic
can you mention how to handle an empty input here?
2

Updated to address index out of range error:

# use python ternary operator to set default value
# index goes out of range
#c = x[2] if x[2] is not None else 0
# use array length instead
b = x[1] if len(x) >= 2 else 0
c = x[2] if len(x) >= 3 else 0

file=open(inputFile, 'a+')
# string concatenation gets rid of repeated function calls
file.write(name + "\t" + a + " " + b + " " + c + "\n")

4 Comments

Doesn't work for me(i'm using Python 2.6)... " File "./test.py", line 99, in get_input c=x[2] if x[2] is not None else 0 IndexError: list index out of range
@Naumann - Updated with different logical test to make sure the value exists.
Thanks Juan, same IndexError: list index out of range...but i see your logic, hope i'll customize that... +1
Aaargh I can see why it probably doesn't work. x[2] gets evaluated before the ternary operator is considered, is my guess. That being the case you might need a heavyweight if block, per parameter, to use this approach. Alexander's answer is starting to look like a better option.
1

You could check the length of the list returned by line.split(' ') to see how many you got, and go from there. Or you could check if an entry in the list is None before assigning it.

For writing to files, the best thing to do is to set the structure of your data and then call write only once, as that is what will bottleneck your efficiency in file I/O.

3 Comments

issue is if user enters a space, it doesn't work, so checking for alternate logics
You could use the strip() function to clean the user input, and then create the list: x.strip().split(" ")
yes, you are right, i'm still adapting to python. really great experience so far
1
#use length of list for checking the input and continue with your operation
line = raw_input("Enter three parm values: ")
x=line.split(' ')
a=b=c=0
fa=fb=fc=0.0 
listLen = len(x)
if(listLen == 3):
    a, b, c = x[0], x[1], x[2]
elif(listLen == 2):
    a, b = x[0], x[1]
elif(listLen == 1):
    a = x[0]

1 Comment

that is nice to have an alternate solution. only issue would be if you enter 1 2 3 and then may be a space will crash that since that'll be an empty string

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.