Sam*_*mir -4 python python-3.x
这是我的代码:
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!")
Run Code Online (Sandbox Code Playgroud)
我想我知道,因为line = '',phonic试图分裂它,但那里什么都没有,我该如何解决?
在做其他任何事情之前,你会想要一个条件语句:
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!")
Run Code Online (Sandbox Code Playgroud)
请注意,while line != ''可以简单地缩短while line,因为''被认为是False,那么!= False就是== True,这是可以根除的.