Python 在循环时重复,直到取消
我刚开始学习Python。上面的代码不正确,有人能帮我修一下吗?基本上,如果答案为“是”,我只想重复循环一次,如果答案为“否”,则中断循环。返回True/False不会返回while循环?它只需要一些小的修正:Python 在循环时重复,直到取消,python,Python,我刚开始学习Python。上面的代码不正确,有人能帮我修一下吗?基本上,如果答案为“是”,我只想重复循环一次,如果答案为“否”,则中断循环。返回True/False不会返回while循环?它只需要一些小的修正: countries={'TW':'Taiwan','JP':'Japan','AUS':'Australia'} def add_country(): while True: new_short=raw_input('Country Name in short
countries={'TW':'Taiwan','JP':'Japan','AUS':'Australia'}
def add_country():
while True:
new_short=raw_input('Country Name in short:')
new_full=raw_input('Country Name in full:')
countries[new_short]=new_full
answer=raw_input('want to add more?')
if answer in ('yes'):
return True
if answer in ('no'):
return False
print countries
add_country()
只需稍加修改:
countries={'TW':'Taiwan','JP':'Japan','AUS':'Australia'}
def add_country():
while True:
new_short=raw_input('Country Name in short:')
new_full=raw_input('Country Name in full:')
countries[new_short]=new_full
answer=raw_input('want to add more?')
if answer in ('yes'):
return True
if answer in ('no'):
return False
print countries
add_country()
您需要中断循环,而不是返回函数 它将类似于:
def add_country():
while True:
new_short=raw_input('Country Name in short:')
new_full=raw_input('Country Name in full:')
countries[new_short]=new_full
answer=raw_input('want to add more?')
if answer == 'no':
return False
print countries
add_country()
如果答案=='no',则不需要
部分。return
语句将使您的执行退出函数。您需要中断循环,而不是返回函数
它将类似于:
def add_country():
while True:
new_short=raw_input('Country Name in short:')
new_full=raw_input('Country Name in full:')
countries[new_short]=new_full
answer=raw_input('want to add more?')
if answer == 'no':
return False
print countries
add_country()
如果答案=='no',则不需要部分。return
语句将使您无法执行函数。
- 使用
break
中断while循环:
- 无需测试
答案是否在中('yes',)
,因为
while True
默认情况下,循环将继续循环:
回答“否”
与回答“否”的相同,只会
如果答案为'n'
或'o'
或'no'
,则为真。那可能不是
你是什么意思。最好使用答案==“否”
。如果您添加了逗号,如在answer in('no',)
,则('no',)
将是一个元组,如果answer
等于元组中的一个项,则满足条件。逗号在这里有很多意义李>
- 使用
break
中断while循环:
- 无需测试
答案是否在中('yes',)
,因为
while True
默认情况下,循环将继续循环:
回答“否”
与回答“否”的相同,只会
如果答案为'n'
或'o'
或'no'
,则为真。那可能不是
你是什么意思。最好使用答案==“否”
。如果您添加了逗号,如在answer in('no',)
,则('no',)
将是一个元组,如果answer
等于元组中的一个项,则满足条件。逗号在这里有很多意义李>
return
终止函数。要停止循环,请使用break
def add_country():
while True:
new_short=raw_input('Country Name in short:')
new_full=raw_input('Country Name in full:')
countries[new_short]=new_full
answer=raw_input('want to add more?')
if answer == 'no':
break
print countries
请注意,我也在('no')
中更改了答案,因为这并没有达到您的预期(它检查答案是'n'
还是'o'
)。返回
终止函数。要停止循环,请使用break
def add_country():
while True:
new_short=raw_input('Country Name in short:')
new_full=raw_input('Country Name in full:')
countries[new_short]=new_full
answer=raw_input('want to add more?')
if answer == 'no':
break
print countries
请注意,我也在('no')
中更改了答案,因为这并没有达到您的预期(它检查答案是'n'
还是'o'
)。在答案为“no”之前,您不想返回。Return将结束函数的执行。您想继续循环。在答案为“否”之前,您不想返回。Return将结束函数的执行。您希望继续
循环。您需要使用中断
命令退出循环
countries={'TW':'Taiwan','JP':'Japan','AUS':'Australia'}
def add_country():
while True:
new_short=raw_input('Country Name in short:')
new_full=raw_input('Country Name in full:')
countries[new_short]=new_full
answer=raw_input('want to add more?')
if answer != 'yes':
break
print countries
add_country()
您需要使用break
命令退出循环
countries={'TW':'Taiwan','JP':'Japan','AUS':'Australia'}
def add_country():
while True:
new_short=raw_input('Country Name in short:')
new_full=raw_input('Country Name in full:')
countries[new_short]=new_full
answer=raw_input('want to add more?')
if answer != 'yes':
break
print countries
add_country()
这并不能解决OP的问题。返回语句将中断函数,如果OP有多个问题,OP希望保持循环这并不能解决OP的问题。返回语句将中断函数,如果OP有多个问题,OP希望保持循环为什么不循环条件答案?那么就不需要break
@Wilduck在他的例子中,他是return
从每个案例中退出的。我同意,这是没有意义的,但问题中所述的他的代码的意图是在任何一个条件下返回该条件的状态。Op说“返回真/假不会返回while循环?”我认为他对return
的含义感到困惑。似乎他认为它会返回到循环的开始(而不是从函数返回)。我很确定他的意图是一直添加到列表中,直到用户回答“否”,然后返回。@Wilduck——OP在他的问题中有很多问题。我猜他是想一直保持循环?“返回True/False不会返回while循环”经过更深入的检查,看起来他希望这两个条件都将他返回while循环,这也没有任何逻辑意义!是的,它是这样的,在('Yes')中回答
相当于在'Yes'中回答
,如果回答='y'或回答='e'或回答='s',
,则为真。这需要更改为answer==“yes”
或answer in('yes',)
(注意逗号,它是一个元组)。为什么不循环条件answer
?那么就不需要break
@Wilduck在他的例子中,他是return
从每个案例中退出的。我同意,这是没有意义的,但问题中所述的他的代码的意图是在任何一个条件下返回该条件的状态。Op说“返回真/假不会返回while循环?”我认为他对return
的含义感到困惑。似乎他认为它会返回到循环的开始(而不是从函数返回)。我很确定他的意图是一直添加到列表中,直到用户回答“否”,然后返回。@Wilduck——OP在他的问题中有很多问题。我猜他是想一直保持循环?“返回真/假不会返回while循环”经过更深入的检查,看起来他希望这两个条件都将他返回while循环