Argh, ok I don't really want to have to ask this here, but here it goes.
I am worried about what will happen if someone leaves a blank or uses a string of text to answer any of the questions in the program, although for testing purposes I'm only dealing with the stockfees object for now. If I'm looking for float values, but receive either a blank or anything other than a float then it'll cause an error.
Here's the part in question:
cash = float(raw_input("How much money do you want to spend? "))stockprice = float(raw_input("What is the share price of the stock? "))cashgoal = float(raw_input("How much money do you want to make as profit? "))stockfees = str(raw_input("What is the stock transaction fee (if applicable)? ") #Here I'm asking for a str and not a float""" I need to put something here to convert the stockfees str into eithera float(0) (if the user inputs anything other than an int/float) or to convert the str into a float using stockfees = float(stockfees)"""print""print "This is how much money you have to spend: $%0.2f" % cashprint "This is how much each share costs: $%0.5f" % stockpriceprint "This is how much you want to make: $%0.2f" % cashgoalprint "This is how much each stock transaction costs: $%0.2f" % stockfees
I know you can simply convert a str to float using stockfees = float(stockfees), but this doesn't help me if the stockfees str is the word 'zero' or simply a blank.
I thought about using an if/then statement (I don't like these statements)
#first I define my compare/test objectstockfees2 = ()#python complains about the = sign on the next line, I don't know whyif stockfees = stockfees2: #if stockfees was left blank stockfees = float(0) #convert stockfees to a float(0)else: stockfees=float(stockfees) #assume stockfees was typed as an int and convert to float
Ok, but what if someone typed a string into the stockfees prompt?
I suppose the best solution would be to not accept strings in any of the inputs and simply inform the user to type int/numbers only (I'll have to learn how to do that). I still am not quite sure how to properly get the rest of the above code to work. I don't know why the if/then statement won't accept my = sign on the 'if' line.
I've been reading for the past couple of hours on how to properly use if/then statements in python, but I haven't found a code examples using the '=' sign, only the '=='. I thought the '==' meant not equal to? Python accepts the '=' sign everywhere else, but not on the 'if' line of the if/then statement from what I can tell. I wasn't able to find any docs anywhere explaining that to me.