我想在python中编写一个名为cont()的函数,它发送一条消息“continue”,它不应该带参数。函数应该打印消息yes,y或Y.then返回true,否则为false。我已经完成了但是有一个错误。
def cont():
x = input("enter any word of your choice")
for letter in 'x':
if letter == '[0]':
a = input ("continue")
if a == 'yes' == 'y' == 'Y' :
continue
print 'current letter will be:',letter
else:
print x
if __name__=="__main__":
cont()
答案 0 :(得分:0)
遗憾的是,您提供的代码和您编写的描述彼此不一致。没有尝试return
任何价值,事实上,没有任何内容可以暗示True
或False
的决定将基于什么。
假设您使用的是python 2.7,这应该可以帮助您入门:
def cont():
x = raw_input("Enter any word of your choice: ")
for letter in x:
# if letter == '[0]':
a = raw_input ("Continue? ")
if a == 'yes' or a == 'y' or a == 'Y' :
# continue
print 'Current letter will be:',letter
else:
print x
break
if __name__ == '__main__':
cont()
我不知道你试图用if letter == '[0]':
或continue
行做什么,所以我已将它们评论出来。
如果您希望返回一个值,只需在代码中选择return True
或return False
即可。另一个选项是将变量设置为True
或False
,然后,cont()
的最后一个语句将return
关键字与变量一起使用。
注意:如果您使用python3,则raw_input
变为input
,print
命令变为print()