[英]i want to write a function in python named cont() which sends a message "continue" and which should take no argument
i want to write a function in python named cont() which sends a message "continue" and which should take no argument.the function should print the message yes,y or Y.then return true otherwise false.我想在 python 中编写一个名为 cont() 的函数,它发送一条消息“继续”并且不应该带任何参数。该函数应该打印消息 yes、y 或 Y。然后返回 true 否则返回 false。 i have done it but there is an error.
我已经做到了,但有一个错误。
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()
Sadly the code that you supplied and the description that you wrote, do not agree with each other.遗憾的是,您提供的代码和您编写的描述彼此不一致。 There is no attempt to
return
any value, indeed, there is nothing to suggest what the decision of True
or False
would be based upon.没有尝试
return
任何值,实际上,没有任何内容表明True
或False
的决定将基于什么。
Assuming that you are using python 2.7, this should get you started:假设您使用的是 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()
I have no idea what you were attempting to do with the if letter == '[0]':
or the continue
lines, so I have commented them out.我不知道你试图用
if letter == '[0]':
或continue
行做什么,所以我已经将它们注释掉了。 If you wish to return a value, simply return True
or return False
at the point of your choosing in the code.如果您希望返回一个值,只需在您在代码中选择的位置
return True
或return False
。 The other option is to set a variable to True
or False
and then, as the last statement of cont()
use the return
keyword with the variable.另一种选择是将变量设置为
True
或False
,然后,作为cont()
的最后一条语句,将return
关键字与变量一起使用。
Note: if you are using python3 the raw_input
becomes input
and the print
commands become print()
注意:如果你使用 python3,
raw_input
变成input
, print
命令变成print()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.