有沒有辦法在 Python 中將輸出與輸入函數(shù)分開?例如,假設(shè)我們要插入一個數(shù)字和名稱。input('Give number and name:')Give number and name:14,John'14,John'我們得到'14,John'。有辦法采取'14','John'嗎?
2 回答

慕少森
TA貢獻(xiàn)2019條經(jīng)驗 獲得超9個贊
采用 .split()
>>> input('Give number and name: ').split(',')
Give number and name: 14,John
['14','John']
要么
>>> number, name = input('Give number and name: ').split(',')
Give number and name: 14,John
>>> number
'14'
>>> name
'John'
請注意,它們都是 strings

慕尼黑8549860
TA貢獻(xiàn)1818條經(jīng)驗 獲得超11個贊
這行得通嗎?
user_input = input("Please write a number then a name, separated by a comma: ")
number, name = user_input.split(", ")
添加回答
舉報
0/150
提交
取消