我想在python中编写一个名为cont()的函数,它发送消息“continue”,不应该参数

时间:2016-09-11 06:42:12

标签: python python-2.7 python-3.x

我想在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()

1 个答案:

答案 0 :(得分:0)

遗憾的是,您提供的代码和您编写的描述彼此不一致。没有尝试return任何价值,事实上,没有任何内容可以暗示TrueFalse的决定将基于什么。
假设您使用的是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 Truereturn False即可。另一个选项是将变量设置为TrueFalse,然后,cont()的最后一个语句将return关键字与变量一起使用。

注意:如果您使用python3,则raw_input变为inputprint命令变为print()