I need help with an online tutorial (this is not homework)
http://www.upriss.org.uk/python/session3.html

It is something very simple, but I am a beginner so I don't understand yet.

Write a script that asks someone to input their first name, last name and phone number. If the user does not type at least some characters for each of these, print "Do not leave any fields empty" otherwise print "Thank you". (Hint: if a variable is empty, its value will be "false".)

So, this is the solution page:
http://www.upriss.org.uk/python/answers3.txt
And the correct implementation is this:

fname = raw_input ("Please, type in your first name: ")
lname = raw_input ("Please, type in your last name: ")
phone = raw_input ("Please, type in your phone number: ")

if fname and lname and phone:
     print "Thank you!"
else:
     print "Do not leave any fields empty"

Why is that the code that I wrote does not work:

a = raw_input("Enter your first name:")
b = raw_input("Enter your last name:")
c = raw_input("Enter your phone number:")
print a
print b
print c
d = (a and b and c)
print d
if d == 0:
	print "Do not leave any fields empty"

d would never get printed, and the "Do not leave any fields empty" never gets printed either.

So, when I omit to type something into one of these variables, what is its actual value.
Is it the number zero, is it the string "0", is it a special value called "false", or is it something else?

Recommended Answers

All 4 Replies

It is considered a null string, which is ''.

a = raw_input("Enter your first name:")
b = raw_input("Enter your last name:")
c = raw_input("Enter your phone number:")
print a
print b
print c
d = (a and b and c)
print d
if d == '':
	print "Do not leave any fields empty"

Okay, when you are prompted, type nothing, and just press enter, the variable is set to the following: ""

As for the reason why your code doesn't work:

The line "d = (a and b and c)" is actually doing the following:

d = a
d = b
d = c

So, it is overwriting the value of d three times. Unless I am mistaken, this is not what you want. This is more like what you want (this is the "long way")

a = raw_input("Enter your first name:")
b = raw_input("Enter your last name:")
c = raw_input("Enter your phone number:")

if a == "":
	print "You did not give your first name."
if b == "":
	print "You did not give your last name."
if c == "":
	print "You did not give your last name."

print "Thank you!"

Post back if you have any more questions.

Logic statements can be confusing at times. A slight change and it will work

a = raw_input("Enter your first name:")
b = raw_input("Enter your last name:")
c = raw_input("Enter your phone number:")
print a
print b
print c
d = (a and b and c)
print d
if not d:
    print "Do not leave any fields empty"

Well, my mistake, vegaseat was correct. I got confused as the "print d" statement printed out only the "c" variable so I assumed that it was only set to "c". What?! Python...not-intuitive? That's a first!

Thank you for the correction vegaseat.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.