大家好,我有一個關于字串決議的問題,這是我到目前為止的代碼:
sentence =' '
while sentence != 'q':
sentence = input('Please enter your input: ').split(',')
if len(sentence) > 1:
print('First word: {sentence[0]}')
print('Second word: {sentence[1]}')
continue
elif len(sentence) == 1:
print('Error no comma.')
continue
elif sentence == 'q':
break
如果沒有輸入逗號,輸出將給我以下內容:
Enter input string:
Jill Allen
Error: No comma in string.
它會一直問我一個字串,直到我輸入 q 退出并且程式退出如下:
Enter input string:
Jill, Allen
First word: Jill
Second word: Allen
Enter input string:
Golden , Monkey
First word: Golden
Second word: Monkey
Enter input string:
Washington,DC
First word: Washington
Second word: DC
Enter input string:
q
我的問題是我無法退出無限回圈。誰可以幫我這個事?我認為程式無法區分 'Jill Allen' 和 'q',因為它們都有 len(sentence) == 1,這就是問題所在,因為它給了我 Error no comma 并一遍又一遍地要求輸入。
uj5u.com熱心網友回復:
您必須更改最后兩個 if 陳述句的位置。因為最后一個 if 陳述句永遠不會被執行,就好像你拆分了 just 的句子一樣q,所以它的長度是 1。
sentence =' '
while sentence != 'q':
sentence = input('Please enter your input: ').split(',')
if len(sentence) > 1:
print('First word: {sentence[0]}')
print('Second word: {sentence[1]}')
continue
elif sentence == 'q':
break
elif len(sentence) == 1:
print('Error no comma.')
continue
uj5u.com熱心網友回復:
檢查此代碼。它對我來說很好。
def wordPrinter(text):
if len(sentence) > 1:
print(f'First word: {sentence[0]}')
print(f'Second word: {sentence[1]}')
elif len(sentence) == 1:
print('Error no comma.')
while True:
sentence = input('Please enter your input: ').split(',')
if sentence == ["q"]:
break
else:
wordPrinter(sentence)
uj5u.com熱心網友回復:
我認為理解你的問題的關鍵可能在這里:
sentence = input('Please enter your input: ').split(',')
您將用戶輸入存盤在字串中,但隨后您呼叫了字串方法 str.split()。此方法基本上根據您作為引數傳遞的分隔符回傳子字串串列。如果沒有分隔符,該方法將改為創建一個串列,其唯一元素是原始輸入字串。
您可以在此處找到有關此方法的更多資訊:https ://docs.python.org/3/library/stdtypes.html#str.split 。
因此,如果您的輸入是“q”,則分隔符將存盤陣列 [“q”],因為沒有逗號。而這個陣列的長度是1,所以會進入第一個“elif”,然后執行“continue”,從而結束當前的迭代。
在沒有關于您的專案的更多資訊的情況下,如果您需要這樣做,您可以更改最后兩個條件的順序和 break 條件本身以使其作業:
sentence =' '
while True:
sentence = input('Please enter your input: ').split(',')
if len(sentence) > 1:
print('First word: {sentence[0]}')
print('Second word: {sentence[1]}')
continue
elif sentence[0] == 'q':
break
elif len(sentence) == 1:
print('Error no comma.')
continue
我還更改了 while 條件,因為它與“break”陳述句是多余的。
uj5u.com熱心網友回復:
當您從一開始就拆分輸入時,您必須逐個串列元素比較字串。
代碼:
sentence =' '
while True:
sentence = input('Please enter your input: ').split(',') #['q']
if len(sentence) > 1:
print(f'First word: {sentence[0]}')
print(f'Second word: {sentence[1]}')
continue
elif sentence == ['q']:
break
else:
print('Error no comma.')
continue
uj5u.com熱心網友回復:
while (sentence := input('Please enter your input: ')) != 'q':
first, *rest = sentence.split(',')
if rest:
print(f'First word: {first}')
second, *rest = rest
print(f'Second word: {second}')
if rest:
print(f'Rest: {",".join(rest)}')
else:
print('Error no comma.')
您可以使用 Python 3.8 的海象運算子將輸入分配給變數并檢查它的值。然后我使用可迭代解包來獲取 the 的第一個元素,str.split其余元素作為串列。在條件中檢查 alist本身會告訴您它是否包含任何元素。我對第二個詞做了同樣的事情。此外,如果有的話,我還會列印其余的單詞。
uj5u.com熱心網友回復:
當您使用 split() 時,您將獲取串列,因此句子不能是 str('q')
試試這樣:
sentence=' '
while True:
sentence = input().split(',')
if (len(sentence) == 1)&(sentence[0] == 'q'):
break
elif len(sentence)<=1:
print('error')
else:
print(f'First word: {sentence[0]}')
print(f'Second word: {sentence[1]}')
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/520229.html
標籤:Python细绳解析
