-4

これは私のコードです:

line = ' '
while line != '':
    line = input('Line: ')
    phonic = line.split()
    start = phonic[0]
    start_4 = phonic [3]
    a = start[0]
    if start_4.startswith(a):
        print('Good!')
    else:
        print("That's not right!")

フォニックはそれを分割しようとしていline = ''ますが、そこには何もありません。どうすれば修正できますか?

4

2 に答える 2

5

他のことを行う前に、条件ステートメントを作成する必要があります。

line = ' '
while line:
    line = input('Line: ')
    if not line:
        break # break out of the loop before raising any KeyErrors
    phonic = line.split()
    start = phonic[0]
    start_4 = phonic [3]
    a = start[0]
    if start_4.startswith(a):
        print('Good!')
    else:
        print("That's not right!")

はと単純にwhile line != ''短縮できることにwhile line注意してください。''False!= False== True

于 2013-08-19T07:11:19.460 に答える
0

Use while True to create an infinite loop, and use break to end it. Now you can end the loop as soon as you read an empty line and not fail when trying to address elements that won't exist:

while True:
    line = input('Line: ')
    if not line:
        break

    phonic = line.split()
    start = phonic[0]
    start_4 = phonic [3]
    a = start[0]
    if start_4.startswith(a):
        print('Good!')
    else:
        print("That's not right!")

Note you don't even have to test more than just if line; an empty string is considered false in a boolean test like if.

于 2013-08-19T07:14:13.260 に答える