Home > Net >  Homework for school not working spams or just dont work
Homework for school not working spams or just dont work

Time:11-15

name = input ("Enter name :")
gender = input("are you a m/M or a f/F:")
while gender != 'M' or gender != 'm' or gender != 'f' or gender != 'F':
    if gender in ['M', 'm']:
        print("Hello Mr",name,",")
if gender in ['F', 'f']:
    print("Hello Miss",name,",")

cant seem to fix it if I do M or m it spams Hello Mr (name) tried it with female just dont do anything

CodePudding user response:

If the gender is m for example, then the condition while gender != 'M' or gender != 'm' or gender != 'f' or gender != 'F': is always True so it'll execute the code below the while block infinite times. In fact you don't even need the while statement for this program.

gender = input("are you a m/M or a f/F:")
if gender in ['M', 'm']:
    print("Hello Mr",name,",")
if gender in ['F', 'f']:
    print("Hello Miss",name,",") 

I could have used if-elif-else construct as well.

name = input ("Enter name :")
gender = input("are you a m/M or a f/F:")
if gender in ['M', 'm']:
    print("Hello Mr",name,",")
elif gender in ['F', 'f']:
    print("Hello Miss",name,",")
else:
    print("Invalid Input")

CodePudding user response:

You're looking for something like this?
It allows you to give inputs two times and print the output. You can change the condition to give more inputs than 2.

i = 0
while i <2:
    name = input ("Enter name :")
    gender = input("are you a m/M or a f/F:")
    if gender in ['M', 'm']:
        print("Hello Mr",name,",")
    elif gender in ['F', 'f']:
        print("Hello Miss",name,",")
    i  = 1
  • Related