Reputation: 361
I'm using the below code as a part of something to price an option in Python. It might be the case that this is an inefficient way to get user inputs. I'll take some advice on that. The below code works though.
But my main concerns are as follows:
1) How can I ensure that the user only types "put" or "call" in the first input? I'd like for the loop to restart if anything else is entered
2) How can I go back to just an individual part if they enter something wrong, instead of restarting the whole loop? For example, if they enter everything in the correct format but then mess up on "K" it will restart the entire loop from the beginning. Is there a way to just re-prompt for K and not have to re-enter everything else?
Thanks in advance.
while True:
call_or_put = raw_input("Enter call or put for option type: ")
print "-------------------------"
S = raw_input("Enter a valid price for the underlying asset: ")
try:
S = float(S)
except ValueError:
continue
print "---------------------------"
r = raw_input("Enter the risk-free rate (as a DECIMAL), using 10 year treasury yield: ")
try:
r = float(r)
except ValueError:
continue
print "---------------------------"
t0 = raw_input("Enter a valid number of days (as an integer) until expiration: ")
try:
t0 = int(t0)
except ValueError:
continue
print "---------------------------"
K = raw_input("Enter the strike price: ")
try:
K = float(K)
except ValueError:
continue
if type(S)==float and type(r)==float and type(t0)==int and type(K)==float:
break
Upvotes: 0
Views: 2193
Reputation: 16875
Just do the sort of thing you've already been doing when you check values. The comparison method may be different, but the overall strategy is the same:
call_or_put = raw_input("Enter call or put for option type: ").strip().lower()
if call_or_put not in ("call","put"):
continue
As for doing over each section rather than restarting, that implies a loop. You could do something like this for each query:
while(True):
print "-------------------------"
S = raw_input("Enter a valid price for the underlying asset: ")
try:
S = float(S)
break
except ValueError:
continue
Upvotes: 1
Reputation: 720
I would recommend you use regex for situations like this but since it isn't really needed I'll explain without it.
1) How can I ensure that the user only types "put" or "call" in the first input? I'd like for the loop to restart if anything else is entered
You can do this many ways, the easiest way would be just check if their input is call or put.
call_or_put = raw_input("Enter call or put for option type: ")
if (call_or_put in ['call','put']):
#Do something
This is error prone as they could accidently put a space or write it in all caps. To get around this I would replace all spaces with nothing and then lowercase all letters in the string.
call_or_put = raw_input("Enter call or put for option type: ").replace(" ","").lower()
if (call_or_put in ['call','put']):
#Do something
You can also use .strip()
instead of .replace()
if you only want to strip leading/ending characters.
2) How can I go back to just an individual part if they enter something wrong, instead of restarting the whole loop? For example, if they enter everything in the correct format but then mess up on "K" it will restart the entire loop from the beginning. Is there a way to just re-prompt for K and not have to re-enter everything else?
You could do this with a while
statement. Here is an example:
answer = raw_input("Please input '123' > ").strip()
while not(answer == "123"):
print "That was not correct, please try again"
answer = raw_input("Please input '123' > ").strip()
print "Thanks!"
Upvotes: 1