Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
If Statement Help
#1
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 Big Grin Big Grin ). 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
Reply
#2
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 + "")
Reply
#3
(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:
if is_male and name:
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 need
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})
Recommended Tutorials:
Reply
#4
(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 ??

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 + "")

Hello, thank you for your help, you have resolved my problem! Many thanks!
Reply
#5
(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 ??

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 + "")

Hello, thank you for your help, you have resolved my problem! Many thanks!

ok, please read https://python-forum.io/Thread-If-Statem...6#pid87556
Reply
#6
(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

Quote:
if is_male and name:
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 need
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})

Thank you for your kind help! Big Grin 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.
Reply
#7
The latter part about string formatting would be more defined here in our tutorial.
Recommended Tutorials:
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020