1

I'm new to learning Python and I'm making a lot of questions these days. I tried to make a Bulls and Cows game, but I failed and then I searched on the internet for the code. I found this sentence and I don't know what it does:

while True:
        guess = raw_input('\nNext guess [%i]: ' % guesses).strip()
        if len(guess) == size and \
           all(char in digits for char in guess) \
           and len(set(guess)) == size:
           break
        print "Problem, try again. You need to enter %i unique digits from 1 to 9" % size

I don't understand the \, what exactly evaluates the Boolean and what does char mean in all() also there is one more \. I'm a bit confused.

I will leave the rest of the code here:

import random

digits = '123456789'
size = 4
chosen = ''.join(random.sample(digits,size))
#print chosen # Debug
print '''I have chosen a number from %s unique digits from 1 to 9 arranged in a random order.
You need to input a %i digit, unique digit number as a guess at what I have chosen''' % (size, size)

guesses = 0
while True:
    guesses += 1
    while True:
        # get a good guess
        guess = raw_input('\nNext guess [%i]: ' % guesses).strip()
        if len(guess) == size and \
           all(char in digits for char in guess) \
           and len(set(guess)) == size:
           break
        print "Problem, try again. You need to enter %i unique digits from 1 to 9" % size
    if guess == chosen:
        print '\nCongratulations you guessed correctly in',guesses,'attempts'
        break
    bulls = cows = 0
    for i in range(size):
        if guess[i] == chosen[i]:
            bulls += 1
        elif guess[i] in chosen:
            cows += 1
    print '  %i Bulls\n  %i Cows' % (bulls, cows)
mhatch
  • 4,441
  • 6
  • 36
  • 62
iMasck
  • 103
  • 1
  • 1
  • 13
  • 3
    Possible duplicate of [How can I do a line break (line continuation) in Python?](http://stackoverflow.com/questions/53162/how-can-i-do-a-line-break-line-continuation-in-python) – Akshat Mahajan Sep 08 '16 at 23:29
  • That is to break a long line of condition into multiple lines to improve readability. Its considered a good programming practice. – stuartnox Sep 08 '16 at 23:30
  • @stuartnox And what mean the lines after the first AND? – iMasck Sep 08 '16 at 23:44
  • do you want to know what `\` is doing after `and` or you want to understand that condition ? – stuartnox Sep 08 '16 at 23:47
  • @stuartnox I want to understand the condition – iMasck Sep 08 '16 at 23:56
  • `all(char in digits for char in guess)` it checks if every `char` from `guess` exists in `char` in `digits`. `len(set(guess)) == size` it checks if length of unique elements of `guess` equals `size` – stuartnox Sep 08 '16 at 23:58
  • @stuartnox Thank you man! – iMasck Sep 09 '16 at 00:35

1 Answers1

1

Typically, code in python needs complete itself on one line. If you would, instead, like to have line breaks to continue an expression to the next line (the most obvious reason being to increase readability) then you can insert a \ at the end of the line.

This tells python to treat the next line as if it is a part of the existing line.

Adam Hopkins
  • 6,837
  • 6
  • 32
  • 52
  • And what mean the lines after the first AND? – iMasck Sep 08 '16 at 23:44
  • 2
    Totally unrelated but I love your code for `class Brewmaster`... ;) – J. Allan Sep 08 '16 at 23:55
  • @eduard the all returns true if everything inside that generator is true. So in this instance it looks like it is taking the input guess and checking that every character in the guess is a character that appears in the digits variable. Therefore looking at this code if guess contained the letter F for example, it would return false. – Adam Hopkins Sep 09 '16 at 00:09
  • @TheBrewmaster Thank you very much, the explication was really easy to understand :D – iMasck Sep 09 '16 at 00:36