Home > OS >  I'm writing a Python code to display simple feedback using user input
I'm writing a Python code to display simple feedback using user input

Time:09-13

I'm trying to use If, elif and else to display user input but i keep running into trouble, the wrong code keeps on getting displayed. The code is displayed below.

print('Are you a new patient? ')
new_patient = input('Please enter True or False: ')
if new_patient != 'True' or 'False':
print('Command not recognized.')
elif new_patient == 'True':
name = print(input('What is your name: '))
print(f'Welcome to our clinic {name} we are happy to have you.')
elif new_patient == 'False':
    print('Already in our database')
else:
 print('Thanks for checking us out.')`

CodePudding user response:

One of your issues is your indentation. The second is because of your if statement. Right now your first if statement code behaves like this:

if new_patient != 'True':
    print('Command not recognized.')
if 'False':
    print('Command not recognized.')

if 'False': will always be True, because all strings are considered truthy except when it is empty ''.

I think this is what you are actually trying to do.

print('Are you a new patient? ')
new_patient = input('Please enter True or False: ')
if new_patient != 'True' and new_patient != 'False':
    print('Command not recognized.')
elif new_patient == 'True':
    name = print(input('What is your name: '))
    print(f'Welcome to our clinic {name} we are happy to have you.')
elif new_patient == 'False':
    print('Already in our database')
else:
    print('Thanks for checking us out.')`

This is a slightly more readable way of writing it.

print('Are you a new patient?')
new_patient = input('Please enter True or False: ')

if new_patient == 'True':
    name = print(input('What is your name: '))
    print(f'Welcome to our clinic {name} we are happy to have you.')
elif new_patient == 'False':
    print('Already in our database')
else:
    print('Command not recognized.')
print('Thanks for checking us out.')

CodePudding user response:

There's a few things wrong with this but none that can't be solved by reading the documentation or going through the Python tutorial for information on on how to use an if statement and general indentation rules.

Firstly

if new_patient != 'True' or 'False':

does not do what you think it does. It will always evaluate to 'False' because the string itself is a truthy value. If you want to check whether it's not either of those you need to check for both conditions separately. Additionally, you need to have indentation after those if and elif statements to do something if the condition in the statement is fulfilled.

else:
    print('Thanks for checking us out.')`

this would never run since if a value is not 'False' and not 'True', your third line would print "Command not recognized".

Here's what your code would look like after fixing the above problems:

print('Are you a new patient? ')
new_patient = input('Please enter True or False: ')
if new_patient != 'True' or new_patient != 'False':
    print('Command not recognized.')
elif new_patient == 'True':
    name = input('What is your name: ') 
    print(f'Welcome to our clinic {name} we are happy to have you.')
elif new_patient == 'False':
    print('Already in our database')
  • Related