[Tutor] Python Question
Alan Gauld
alan.gauld at yahoo.co.uk
Mon Jul 5 04:11:24 EDT 2021
On 05/07/2021 00:55, riskxexbiz--- via Tutor wrote:
> Here is the python code I am using but it is not working correctly:
Please always post in plain text otherwise the mail system
mangles the code as below...
> # Ensure investor type is either conservative, moderate or aggressive if investor != ["conservative" or "moderate" or "aggressive"]: error_statement = "Investor Type Must Be Either Conservative, Moderate or Aggressive!" return render_template("information.html", error_statement=error_statement, investor=investor)
Picking out one line:
> if investor != ["conservative" or "moderate" or "aggressive"]:
This does not do what you want. Python evaluates the bit
inside the list as [(True or True or True)] so your code
is read by Python as:
if investor != [True]
Instead you probably want to use the 'in' operator:
if investor not in ["conservative", "moderate", "aggressive"]:
However, investor must match the test strings exactly so
it's usually a good idea to make the case consistent
and remove any extra whitespace:
if investor.strip().lower() not in ["conservative",
"moderate",
"aggressive"]:
(wrapping is just to fit in the email message!)
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list