If Statement Help - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: If Statement Help (/thread-20170.html) |
If Statement Help - VictorVictus - Jul-30-2019 Hello, users of Python! My name is Victor Puche, I'm a total noob at programming in Python. I'm trying to create a test program which, using variables called is_male and is_female. Simple stuff. The Program asks you for a name, and the question changes depending if the variables are indicating that the name should be a male one or a female one. My problem starts when I want to add a reference to a videogame if you type a male name (Connor ). How could I make it so it says a different phrase that the one I've set it to print? I leave the code here so you can see for yourselves. is_male = True is_female = False if is_male and not(is_female): name =(input("Please State My Name! It must be a Male name:")) if is_male and name: "Connor" print("My Name Is Connor, I'm The Android Sent by Cyberlife!") if is_male and not name: "Connor" print("My name is " + name + "")This, ofcourse doesn't work, this is the report when you run this: Please State My Name! It must be a Male name:Connor My Name Is Connor, I'm The Android Sent by Cyberlife! My name is Connor Please State My Name! It must be a Male name:Connor My name is Connor Process finished with exit code 0 As you can see, it gives you both the reference and the default one, and it asks you again for the same thing but doesn't give any reference when typed at. It also happens with other things that you type. Could somebody help me? I would be extremely grateful RE: If Statement Help - cvsae - Jul-30-2019 i dont know if understand correcly but i wil try to help cann you provided the expected results ?? try this is_male = True is_female = False if is_male and not(is_female): name =(input("Please State My Name! It must be a Male name:")) if is_male and name == "Connor": print("My Name Is Connor, I'm The Android Sent by Cyberlife!") elif is_male and name != "Connor": print("My name is " + name + "") RE: If Statement Help - metulburr - Jul-30-2019 (Jul-30-2019, 12:56 PM)VictorVictus Wrote: if is_male and name: "Connor"This is not doing what you think it is doing. I would highly suggest to read through out common pitfalls as well as validating user input Quote:This is what you have. This if condition will always execute because of name. It will always have content in it from the user input (unless the user just hits enter instead). It does not mean if name equals conner...for that you needif is_male and name: if name == "Conner":Also as added advice. Your second if condition could simply be an else clause due to it being the opposite if the initial if condition. Also you should not concatenate your strings like this. It is not pythonic. Quote:print("My name is " + name + "")You should use python format or f-string of python3.6+ print("My name is {}.format(name))f-string print(f"My name is {name}) RE: If Statement Help - VictorVictus - Jul-30-2019 (Jul-30-2019, 01:03 PM)cvsae Wrote: i dont know if understand correcly but i wil try to help cann you provided the expected results ?? Hello, thank you for your help, you have resolved my problem! Many thanks! RE: If Statement Help - cvsae - Jul-30-2019 (Jul-30-2019, 01:12 PM)VictorVictus Wrote:(Jul-30-2019, 01:03 PM)cvsae Wrote: i dont know if understand correcly but i wil try to help cann you provided the expected results ?? ok, please read https://python-forum.io/Thread-If-Statement-Help?pid=87556#pid87556 RE: If Statement Help - VictorVictus - Jul-30-2019 (Jul-30-2019, 01:11 PM)metulburr Wrote:(Jul-30-2019, 12:56 PM)VictorVictus Wrote: if is_male and name: "Connor"This is not doing what you think it is doing. I would highly suggest to read through out common pitfalls as well as validating user input Thank you for your kind help! Unfortunately, I've not managed to understand a single thing you said :(. Regardless, many thanks! If you could point me in a way to learn more stuff about Python I Will be heavily thankful. RE: If Statement Help - metulburr - Jul-30-2019 The latter part about string formatting would be more defined here in our tutorial. |