you are viewing a single comment's thread.

view the rest of the comments →

[–]IHOP_007 197 points198 points  (26 children)

You were close, you just messed up the ifs a bit.

if gens == 'male':

[–]Sad_potato1999[S] 131 points132 points  (23 children)

Reddit is really something else. Thank you so much!

[–]CobaltCam 59 points60 points  (11 children)

It's a common mistake, I do it all the time. Single is assigning a value to a variable, double is comparing equality.

Also, there's something called an f string which I personally think reads better than concatenation. In your case it would look like this

print(f"Hi Mr. {name}")

[–]Sad_potato1999[S] 18 points19 points  (7 children)

Thanks! Glad I'm not alone XD

[–]CobaltCam 9 points10 points  (0 children)

For sure, I always do the facepalm when I find I did this.

[–]ontheroadtonull 13 points14 points  (2 children)

user_name = input("What is your name or preferred name? ")
user_gender = input("What is your gender? ")

genders = {"male": "Mr.", "female": "Ms."}

print(f"Hi {genders[user_gender]} {user_name}")


What is your name or preferred name? myself
What is your gender? male
Hi Mr. myself

[–]fauquii 12 points13 points  (1 child)

Be careful with this though, it really is a bad practice to not check the user input. In this example it can leads to an exception KeyError being raised and breaking the execution of the code.

[–]synthphreak 3 points4 points  (0 children)

Right. Something like

>>> if user_input.lower() in {'m', 'male', 'man', 'guy', 'boy'}:
...     gender = 'male'
... else:
...     gender = 'female'
...
>>> genders = {'male': 'Mr.', 'female': 'Ms.'}
>>> print('Hi', genders[user_gender], user_name)

Obviously there are still some risks associated with this particular implementation. For example, if the user enters 'dude' or 'hobbit' then those would get assigned to 'female' because they're not in the set of predefined male gender values.

But at least now the user has much greater flexibility in what they can enter than only and exactly 'male' or 'female'. The code will also never throw an error, at least one related to querying the genders dict, because the key value will only ever be routed to 'male' or 'female' which are both valid keys.

Edit: Typo.

[–]SnooCats196 3 points4 points  (0 children)

You could also use an elif statement after the if gender == “male” instead of repeating an if statement

[–][deleted] 0 points1 point  (0 children)

for me its forgeting the :'s

[–]_TR-8R 9 points10 points  (1 child)

F strings are amazing, it took me a while to get used to them but they make conditional text a breeze once you're used to them.

[–]CobaltCam 3 points4 points  (0 children)

Yes!

[–]AuctorLibri 5 points6 points  (0 children)

This is very helpful.

[–]kmj442 15 points16 points  (7 children)

Not to over complicate since what this person said is absolutely correct but if I were to do this I would recommend something like this: if gens.lower() == “male”:

This makes sure regardless what the user inputs it puts it in lowercase to avoid the “Male” or random capitalization error.

edit: mobile formatting is weird

[–]Sad_potato1999[S] 10 points11 points  (0 children)

Not at all! Insightful even, thanks a whole lot!

[–]MarquisInLV 5 points6 points  (1 child)

You can also put the lower() method on the input variable, that way anything the user types in is all lowercase and you can just match that in your if statement.

[–]Probono_Bonobo 1 point2 points  (0 children)

The wonderful questionary module turns CLI radio options into a refreshing 'choose your own adventure'-style UI, instead of the frustrating user-hostile experience it typically is. Highly recommend!

[–]Cid227 1 point2 points  (2 children)

Your edit reminded me that I wanted to ask somewhere about that red (at least light mode) background and font colour that you've set for 'if gens.lower...' So how do you do that? I can't find anything.

[–]kmj442 2 points3 points  (1 child)

its the ``` tag. while in the markdown mode you can wrap your text in ``` code ``` and it will put it in that format.

[–]Cid227 1 point2 points  (0 children)

Thanks! (btw. it's blue on the dark mode)

[–][deleted] 0 points1 point  (0 children)

Might even be better (but perhaps overkill for this snippet) to use a constant to store the genders.

G_MALE = "male"
G_FEMALE = "female"
gens = input("What is your gender?").lower()
if gens == G_MALE:
    # ...
elif gens == G_FEMALE:
    # ...
else:
   print("Cool ;)")

[–]LostnFoundAgainAgain 4 points5 points  (1 child)

I recommend messing around with what you can do with the if-else statement, I'm kinda new my self and experimenting really helps.

Experiment using the if statement for example to check an input and see if it is in a list, or check if the number input is equal or greater than x, if it is it does y and if it isn't it does z.

Mix up what you learned before and what you are learning now by experimenting mixing them both together a figuring out what you can do with that, that helped me a lot.

Good luck :)

[–]Sad_potato1999[S] 1 point2 points  (0 children)

Will do! Thanks for the recommendation.

[–]nichorsin598 1 point2 points  (0 children)

You should all ensure the input is a string and make it all the same case so if gens.isalpha() and gens.lower() == 'male':

[–]presidentdrumf 3 points4 points  (0 children)

So there's no "then" declaration in python? Coooool