-1

im very new to coding so i want to know how to do this, sorry if its a newbie question i want so that where its "str(new_patient) + "" says "you are a new patient!" but instead i got True as a result, the phrase should look like " His name is John Smith he is 20 years old and is a new patient

name = "John Smith"
age = 20
new_patient = True
if new_patient:
    print("a new new_patient")
else:
    print("you are not a new patient")


print("His name is " + name + " he is " +str(age) + " old and is a "+ str(new_patient) + "")
Tom Karzes
  • 22,815
  • 2
  • 22
  • 41
Caio
  • 1
  • 1
  • 1
    ```new_patient="a new new_patient"```. You need to change to value of ```new_patient``` to those which you print –  Jul 22 '21 at 02:40
  • The "new_patient" is a variable name and the boolean object "True" is its value. When you use the function str(), it will use the built-in function __str__() of the "new_patient" and return the value "True" of the "new_patient" as a string. – shiratori3 Jul 22 '21 at 02:51

6 Answers6

1

You can store the text you want to display in a string like this:

age = 20
new_patient = True

new_text = ""
if new_patient:
    new_text = "a new patient"
else:
    new_text = "not a new patient"

print("His name is " + name + " he is " + str(age) + " old and is " + new_text)
Timus
  • 10,974
  • 5
  • 14
  • 28
Lumorti
  • 180
  • 7
1

you should save the str in a variable then you print the value from the variable. Here I save the string in variable:new_patient.then I use the variable in the print phrase

name='Caio'
age = 20
new_patient = True
if new_patient:
    new_patient="a new patient"
    
else:
   new_patient = " not a new patient"


print("His name is " + name + " he is " +str(age) + " years old and is "+ str(new_patient) + "")```
Pengbo Wu
  • 67
  • 8
0
name = "John Smith"
age = 20
new_patient = True
if new_patient:
    new_patient = "a new new_patient"
else:
    new_patient = "you are not a new patient"

print("His name is " + name + " he is " + str(age) + " old and is a " +new_patient)

pppig
  • 1,215
  • 1
  • 6
  • 12
0

You can tually need to assign the strings to new_patient. If you if/else statement, you just print the string and the value of new_patient is unchanged. So True is printed.

name = "John Smith"
age = 20
new_patient = True
if new_patient:
    new_patient="a new new_patient"
else:
    new_patient="you are not a new patient"


print("His name is " + name + " he is " +str(age) + " old and is a "+ str(new_patient) + "")
0

Using f-string (Python 3.6+)

name, age, new_patient = "John Smith", 20, True
print(f"His name is {name} he is {age} old and {'is a new patient' if new_patient else 'you are not a new patient'}")
Алексей Р
  • 7,507
  • 2
  • 7
  • 18
0

Using f-strings in python 3.6+ and a tuple-ternary trick makes this clean. I would also suggest adding gender here, and (not shown) limit choices:

#!/bin/python3.8
def patient(name: str, age: int, gender: str, is_new: bool) -> None:
    print(f"{name} is a {age} year old {gender} who is a{('n existing', ' new')[is_new]} patient.")

patient("John Smith", 20, "male", True)

# prints: John Smith is a 20 year old male who is a new patient.
miigotu
  • 1,387
  • 15
  • 15