Quantcast
Channel: How do I make the for loop work only if one or more items != 'yes' - Stack Overflow
Viewing all articles
Browse latest Browse all 8

Answer by Alex for How do I make the for loop work only if one or more items != 'yes'

$
0
0

There are a few ways to do this:

  1. Use a flag variable and just output at the end. (Slightly inefficient if the first response is a no)
  2. Use a flag variable and a while loop to exit as soon as the user responds with no. (Can be slightly confusing)
  3. Use the builtin any method. (Can be confusing, not recommended)
flag = Truefor i in range(4):    if user[i] != 'y':        flag = False    # User has answered no to something, set the flag to falseif flag:    # User has answered yes to everything    # <do your `yes` output>else:       # User has answered no to something    # <do your `no` output>

Viewing all articles
Browse latest Browse all 8

Latest Images

Trending Articles



Latest Images