0% found this document useful (0 votes)
3 views1 page

For Loops and The Range Function: I'm Fine, Thanks. Who Are You? Joe Mary Joe Swordfish

Uploaded by

Zhirayr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views1 page

For Loops and The Range Function: I'm Fine, Thanks. Who Are You? Joe Mary Joe Swordfish

Uploaded by

Zhirayr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

Run this program and give it some input.

Until you claim to be Joe, it


shouldn’t ask for a password, and once you enter the correct password, it
should exit.
Who are you?
I'm fine, thanks. Who are you?
Who are you?
Joe
Hello, Joe. What is the password? (It is a fish.)
Mary
Who are you?
Joe
Hello, Joe. What is the password? (It is a fish.)
swordfish
Access granted.
for Loops and the range() Function
The while loop keeps looping while its condition is True (which is the reason
for its name), but what if you want to execute a block of code only a certain
number of times? You can do this with a for loop statement and the range()
function.
“Truthy” and “Fa lsey” Values
There are some values in other data types that conditions will consider equivalent
to True and False. When used in conditions, 0, 0.0, and '' (the empty
string) are considered False, while all other values are considered True. For
example, look at the following program:
name = ''
while not name:u
print('Enter your name:')
name = input()
print('How many guests will you have?')
numOfGuests = int(input())
if numOfGuests:v
print('Be sure to have enough room for all your guests.')w
print('Done')
If the user enters a blank string for name, then the while statement’s condition
will be True u, and the program continues to ask for a name. If the value for
numOfGuests is not 0 v, then the condition is considered to be True, and the
program will print a reminder for the user w.
You could have typed not name != '' instead of not name, and numOfGuests
!= 0 instead of numOfGuests, but using the truthy and falsey values can make
your code easier to read.
www.it-ebooks.info

You might also like