Wikipedia:Reference desk/Archives/Computing/2020 February 18

Source: Wikipedia, the free encyclopedia.
Computing desk
< February 17 << Jan | February | Mar >> February 19 >
Welcome to the Wikipedia Computing Reference Desk Archives
The page you are currently viewing is a transcluded archive page. While you can leave answers for any questions shown below, please ask new questions on one of the current reference desk pages.


February 18

What is the problem with this tic-tac-toe game code?

Here is the relevant code:


#!/usr/bin/env python3

if __name__ == '__main__':

    def check_board_fullness(board):
        for item in board:
            if board[item] == ' ':
                return False
            return True
    
    #Creates the board
    board = {'top-L': ' ', 'top-M': ' ', 'top-R': ' ',
                'mid-L': ' ', 'mid-M': ' ', 'mid-R': ' ',
                'low-L': ' ', 'low-M': ' ', 'low-R': ' '}
    def print_board(board):
        print(board['top-L'] + '|' + board['top-M'] + '|' + board['top-R'])
        print('-+-+-')
        print(board['mid-L'] + '|' + board['mid-M'] + '|' + board['mid-R'])
        print('-+-+-')
        print(board['low-L'] + '|' + board['low-M'] + '|' + board['low-R'])
        
    #Function to quit
    def end(move):
        if move == 'quit':
            raise SystemExit
        
    #Function to check if any player has won
    def check_winner(board):
        win_conditions = [
             ['top-L', 'mid-L', 'low-L'],
             ['top-M', 'mid-M', 'low-M'],
             ['top-R', 'mid-R', 'low-R'],
             ['top-L', 'top-M', 'top-R'],
             ['mid-L', 'mid-M', 'mid-R'],
             ['low-L', 'low-M', 'low-R'],
             ['top-L', 'mid-M', 'low-R'],
             ['top-R', 'mid-M', 'low-L']]
        for condition in win_conditions:
            if condition[0] == 'X' and condition[0] == condition[1] == condition[2]:
                return 'X'
            if condition[0] == 'O' and condition[0] == condition[1] == condition[2]:
                return 'O'
            else:
                return False

    turn = 'O'
    while check_board_fullness(board) == False:
        while check_winner(board) == False:
            print_board(board)
            print("It is now",turn,"'s turn to make a move. What is your move?: ")
            inp = input()
            if inp in board and board[inp] == ' ':
                board[inp] = turn
                if turn == 'O':
                    turn = 'X'
                if turn == 'X':
                    turn = 'O'
            else:
                print("That's not a valid move!\n")
                inp = input("Please make another move: ")
                if turn == 'X':
                    turn = 'O'
                if turn == 'O':
                    turn = 'X'
    if check_winner == 'X':
        print("X wins!")
    if check_winner == 'O':
        print("O wins!")
    if check_board_fullness(board) == True and check_winner(board) == False:
        print("It's a tie!")

The main problem with this code that I can see is that it doesn't actually switch the turn from 'O' to 'X' once 'O' actually makes a move. Why exactly is this the case? 68.96.93.207 (talk) 03:06, 18 February 2020 (UTC)[reply]

I think the problem is here:

if turn == 'O':

 turn = 'X'

if turn == 'X':

  turn = 'O'

if turn is O it changes it to X, but the next statement turns X back to O. Bubba73 You talkin' to me? 03:24, 18 February 2020 (UTC)[reply]

I got it; basically, I wrote "if" the second time whereas I was actually supposed to write "elif" (short for "else if") the second time. Thank you! Futurist110 (talk) 22:25, 21 February 2020 (UTC)[reply]

It's slightly controversial but I favor using interactive debuggers to analyze this type of problem. Python comes with a semi-useful one called "pdb", and if you use an interactive development environment (IDE), it might have a debugger of its own. There are various ways you can simplify (refactor) that code as well and it's worth spending some time on that. 2601:648:8202:96B0:0:0:0:7AC0 (talk) 05:50, 18 February 2020 (UTC)[reply]

You're mixing code with your functions and functions should be placed before if __name__ == '__main__': line.
Sleigh (talk) 08:39, 18 February 2020 (UTC)[reply]

I'm pretty sure that condition[0] == condition[1] == condition[2] is not going to do what you expect. It's not going to compare three things and evaluate to "true" if they're all the same. Rather, it's going to evaluate the equality of [0] and [1], and the compare "true" or "false" to [2]. (Or maybe the other way around, compare [1] to [2] first; I don't know how the precedence will go here.) Or is Perl unusual in this regard? Anyway, it's safer and easier and faster simply to say condition[0]=='X' and condition[1]=='X' and condition[2]=='X'. --jpgordon𝄢𝄆 𝄐𝄇 23:39, 18 February 2020 (UTC)[reply]

Regarding the chained comparison (is not going to do what you expect), Python has special handling for chained comparisons: "Comparisons can be chained arbitrarily, e.g., x < y <= z is equivalent to x < y and y <= z". So
>>> 3 == 3 == 3
True
but
>>> 3 == (3 == 3)
False
-- Finlay McWalter··–·Talk 10:51, 19 February 2020 (UTC)[reply]
Wow. Didn't know that one. Still, morally unsafe...I'll shut up now. --jpgordon𝄢𝄆 𝄐𝄇 15:29, 19 February 2020 (UTC)[reply]
The behaviour you describe comes from C, and many post-C languages (including C++, Java, and it would seem Perl) inherited that thinking, I suspect without really considering there's much of an alternative. Haskell works (parses) similarly, but at least has the good grace to fail (because it refuses to silently coerce an integer to a boolean oops, a boolean to an integer). But mathematicians have been doing it the other way for centuries, so it's not obviously wrong. Python has traditionally been quite a bit more agressive about deciding to do things its way, rather than aping C syntax in a decidedly non-C language (and in general, learning Python from a C-ish background, one has to break onesself of a range of C-ish mindsets to write clear, ideomatic Python). So really, for the Python programmer, it's something of a shibboleth - if a < b <= c makes one's brain itchy, one is still thinking in C. -- Finlay McWalter··–·Talk 17:47, 19 February 2020 (UTC)[reply]

Skype's cloud storage

It seems that the inability to delete own Skype conversation persists since 2017, as I still get "some selected messages cannot be removed" on my laptop, Windows 10. I also have some privacy concerns since the calls are stored on a cloud server for 30 days. Is there anything to do about it? Brandmeistertalk 10:45, 18 February 2020 (UTC)[reply]

If you want to delete it locally, you can close Skype, find where it stores its data and open up the database. Every line of conversation is a row in one of the tables. Might also be in multiple tables, I don't remember. No idea how to delete it in the cloud. I suspect it's possible that it will come back if the other conversation member doesn't do the same thing in his Skype db. 89.172.57.225 (talk) 22:21, 18 February 2020 (UTC)[reply]

Snapchat filters

The Snapchat article describes the concept of a "filter" as something different than what I have heard it is. It seems the article should mention putting cat ears on people and that sort of thing. However, I'm not sure what to use as a source or how it would be described.— Vchimpanzee • talk • contributions • 21:45, 18 February 2020‎ (UTC)[reply]

You can always keep it in scare quotes. That's what I usually do when dealing with "social" "media". 89.172.57.225 (talk) 22:22, 18 February 2020 (UTC)[reply]
@Vchimpanzee: I think that is covered by "Snaps can be personalized with various forms of visual effects", but you should start a discussion on the article's talk page if you have ideas for improvement. RudolfRed (talk) 22:23, 18 February 2020 (UTC)[reply]
I'm not convinced those words are enough to communicate the idea.— Vchimpanzee • talk • contributions • 23:00, 18 February 2020 (UTC)[reply]
What's wrong with filter? In image processing (and for that matter in data processing in general), a filter can do pretty much anything to its data, including adding cat ears. --jpgordon𝄢𝄆 𝄐𝄇 03:49, 19 February 2020 (UTC)[reply]
Jpgordon, that is an overly liberal definition of the word. By analogy from real-world items, such as a water filter or a camera lens filter, these are items which take a certain input and remove or add some elements to the output, for an overall subtle change, in most cases. A filter operates on the whole input by default. So, no, if I wanted to add cat ears to someone in a film camera, I would not attach a "cat ears filter" to the lens, I would draw them on in the darkroom. A "visual effect" is a very good description of such a process, and drawn from the film industry where visual effects can consist of scale models, background matte paintings, or CGI animations. (None of those are filters.) — Preceding unsigned comment added by Elizium23 (talkcontribs) 05:09, 19 February 2020 (UTC)[reply]

As an interesting aside, for Snapchat filters refers to things which apply a static effect to an image. E.g. text, colour effects etc [1]. Lenses refers to the AR style effects which operate on elements of the image [2]. However, as can be seen from this e.g. [3], Snapchat lenses are still called filters, as are AR style effects for other companies [4] [5].

@Elizium23: It's not overly liberal since it's the way the word is used in modern times. See for example Filter (software), Filter (video), Email filtering, or [6] [7]. As always, it's a Etymological fallacy to assume just because a word means something in some older contexts, it must mean the same thing in other newer contexts. Languages evolves and adapts.

BTW Snapchat and other such images filters do operate on the whole input. They just selectively detect and modify certain components. Many such filters are able to detect multiple faces, and will apply the same effects to all human faces detected. Some can even detect cat and dog faces.

While it's true it's not really possible to do such things with traditional filters, the idea that their change is subtle or affects the whole image isn't true anyway. For example, the effects of filters on these images is clearly not 'subtle' [8]. Meanwhile, if you imagine a darkened room with two lights, one with a very wide spectrum including in the infrared or UV and one with a narrow spectrum, if you apply a UV or IR selective filter when photographing those lights one light could very well look very similar with or without the filter (depending on the film and how you process it), one light may disappear. Then of course there is significant interest in developing selective filters for various liquid separation and processing e.g. [9] [10]/[11].

Note also I used both words earlier. The result can reasonably be called a visual effect, but it's a visual effect applied by a filter.

Nil Einne (talk) 11:16, 19 February 2020 (UTC)[reply]