0

I'd like to keep asking for the user for isMale if they don't enter True or False. However, the while loop keeps asking even when I enter True or False. I don't know why.

name = input("Name: ")
age = int(input("Age: "))
isMale = bool(input("Male? "))

while (isMale != True) or (isMale != False): #Is this correct?
    print("Wrong Input. Please type True/False")
    isMale = bool(input("Male? "))

if(isMale == True):
    print("His name is "+ name)
    print("He is {input} years old.".format(input= age))
    print("He is a Male")

elif(isMale == False):
    print("Her name is " + name)
    print("She is {input} years old.".format(input= age))
    print("She is a Female")
Umutambyi Gad
  • 4,082
  • 3
  • 18
  • 39
  • @quamrana I think I got that part correct. I tried changing it to that but it accepts even wrong inputs and automatically assume isMale is True – frdspuzi Sep 13 '20 at 13:26
  • Does this answer your question? [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – quamrana Sep 13 '20 at 13:27
  • 2
    @frdspuzi bool('True'), and bool('False') are both going to be True because neither string is of length 0 – Hadrian Sep 13 '20 at 13:30
  • I think the problem stems from ‘bool(input(...))’. It will *only* ever return True or False and never anything else. You probably need you own conversion and not python’ s notion of ‘Truthy’ and ‘Falsy’. – quamrana Sep 13 '20 at 13:32

1 Answers1

0

Here is the simple solution but let me give some explanation when you cast an input to boolean will be like if an input is empty will be false and if an input contains something will be true no matter how the string is will always be true so that in that case I decided to create variable and initialize it to false and if one of the condition evaluates to true the variable called notValid will be false.

And I added some string method that will help you to format an input which are strip() to remove extra white spaces and tittle() which will capitalize an input.

and also all of the conditions are not checking between and input which is the form of string and boolean no I changed isMale == True to isMale == 'True' because both are string will be easy to compare them.

Example

name = input("Name: ")
age = int(input("Age: "))
isMale = input("Male? ").strip().title()
notValid = True

if(isMale == 'True'):
    print("His name is "+ name)
    print("He is {input} years old.".format(input= age))
    print("He is a Male")
    notValid = False

elif(isMale == 'False'):
    print("Her name is " + name)
    print("She is {input} years old.".format(input= age))
    print("She is a Female")
    notValid = False

while notValid:
    if (isMale != 'True') or (isMale != 'False'):
        print("Wrong Input. Please type True/False")
        isMale = input("Male? ").strip().title()
    else:
        notValid = False
Umutambyi Gad
  • 4,082
  • 3
  • 18
  • 39