-1

I've tried multiple means of splitting the tuple into individual items but each time I try it returns the same ('0,1 0,2',) output instead of ('0,1' '0,2'). I need it to be tuple in order to match another input later in the program (which I removed for conciseness).

import PySimpleGUI as sg
P1o=[]
P2o=[]
MAX_ROWS = MAX_COL = 10

def turn():
            tupleP1o = tuple(i for i in P1o)
            print(tupleP1o)
            tupleP2o = tuple(i for i in P2o)
            print(tupleP2o)            
def Battleship1():
    layout = [
                [sg.Text('Player1 please enter your ship positions'), sg.InputText('', size=(10,1), key='input_\P1o')],
                [sg.Submit(), sg.Cancel()]
                          ]
    window = sg.Window('player1 values', layout)
   
    while True:
        event, values = window.read()
        if event == sg.WIN_CLOSED:
            break
        elif event == 'Submit':
            x = values['input_\P1o']
            P1o.append(x)
            window.close()
            #turn()
        elif event == 'cancel':
            window.close()
            break
    layout = [
                [sg.Text('Player2 please enter your ship positions'), sg.InputText('', size=(10,1), key='input_\P2o')],
                [sg.Submit(), sg.Cancel()]
                          ]
    window = sg.Window('player2 values', layout)
    
    while True:
        event, values = window.read() 
        if event == sg.WIN_CLOSED:
            break
        if event == 'Submit':
            y = values['input_\P2o']
            P2o.append(y)
            window.close()
            turn()
            turn_()
        if event == 'cancel':
            window.close()
        break                

2 Answers2

1

If you have a string like '0,1 0,2' and want to turn it into the tuple ('0,1' '0,2') use str.split() to split it at whitespace, then tuple() to convert that list to a tuple.

s = '0,1 0,2'
t = tuple(s.split())
print(t)
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

Your question needs to be clarified.

Do you need a tuple with a one string that is splitted by whitespace character? Maybe you need a tuple that contains tuples of splitted strings?

some_str = ("0,1 2,3 4,5 6,7 8,9")
result = tuple([tuple(i.split(",")) for i in some_str.split()])

Result:

(('0', '1'), ('2', '3'), ('4', '5'), ('6', '7'), ('8', '9'))

or (as suggested by Barmar):

s = '0,1 0,2'
t = tuple(s.split())

Result:

('0,1' '0,2')
qoopdata
  • 11
  • 2
  • The second one would be ideal but when tried that i received the error message: AttributeError: 'list' object has no attribute 'split' – Thunder061005 Jan 19 '23 at 18:33