Champion_Develops
Champion_Develops

Reputation: 21

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

I'm trying to use if, elif, and else statements 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.')`

Upvotes: 0

Views: 909

Answers (2)

Ethicist
Ethicist

Reputation: 827

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 a string 'False' because the string itself is a truthy value, which is why the body of that statement will run every time. 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 fourth line attempts to print "Command not recognized".

name = print(input('What is your name: '))

print does not have a return value, so name is assigned to None. You meant to assign it to the input value instead.

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' and 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')

Upvotes: 1

Alexander
Alexander

Reputation: 17355

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.')

Upvotes: 2

Related Questions