I'm currently coding a text based game in python in which the narrative will start differently depending on answers to certain questions. The first question is simple, a name. I can't seem to get the input to display in the correct text option after the prompt.
I tried using
"if name is True"
and
"if name is str"
but they both skip to the else option, instead of displaying the correct option after input.
while True:
try:
# This will query for first user input, Name.
name = str(input("Please enter your name: "))
except ValueError:
print("Sorry, I didn't understand that.")
# No valid input will restart loop.
continue
else:
break
if name is str:
print("Ah, so " + name + " is your name? Excellent.")
else:
print("No name? That's fine, I suppose.")
So if I enter John as my name, I'd expect the output to be "Ah, so John is your name? Excellent."
But instead I enter John and it outputs:
Please enter your name: John
No name? That's fine, I suppose.
Any help would be appreciated. Thanks.
if name is str:is doing? - Scott Huntername is str-if name.strip():will check if the input is different from whitespaces (i.e. You got some input thats visible) - if name is str checks if name is str not if name is a non-empty string instance - Patrick Artner