How would I fix this if statement?

I’m currently doing the 8-ball project at the end of the second lesson of Python 3 and I want to know how to check if a variable has a value to it. I have:

name = input(“Enter Name:”)

if name == “Sam”:

name = “Sam”

else:

name = “User”

To get a name but it’s not quite right as I’m unsure how to check if a variable has a value? (or whatever the term is).
Basically I want the if statement to check if name has a value and if so name = (input data) else name = “User”. So how do I ask if name has any data in it?

P.S I’m aware input won’t work on the codecademy IDE, its just something I want to do as an extra to the project.

Since you’re working with strings here you can actually just use an if. Strings have a boolean value of True if they contain anything and False if they are empty. For example:

a = ""

while not a:
  a = input()

print(a)

So empty strings such as “” are boolean false while a string like “A” is true?
So if I wanted to do:

if name != True:
name = “user”

Would that do the same thing as while not?

if name != True:
  name = "user"

This would certainly work but note you could also use the conditions:

if not name:

OR

if name == False:
1 Like