即使语法正确,python代码也不会执行

时间:2018-01-02 09:55:07

标签: python

我编写了一个简单的python代码来接受输入并使用该输入应该执行一些函数声明,但它没有显示预期的结果。这是代码:

start = print('Answer yes or no to continue please')
name = input(str('\nIs this it'))

if name == 'yes':
    def f():
        print('This could be it')

    def do_twice(x, f):

        f = input(str('Are you sure this it'))
        if f == yes:
            print('Ok thank you')
            print('Your input is', f)
else:
    pass

2 个答案:

答案 0 :(得分:0)

我假设你想通过用户输入来控制你的程序流程。试试这个:

def badinput():
    name = input(str('Answer yes or no to continue please\nIs this it'))

    if name == 'yes':
        print('This could be it ')

        f = input(str('Are you sure this it '))
        if f == 'yes':
            print('Ok thank you.', 'Your input is: ', f)
        else:
            print('Your input s != \'yes\'')

    elif name == 'no':
        print('Ok thank you. Exit.')
    else:
        print('Your input s != \'yes\'')

答案 1 :(得分:0)

声明功能后,您需要调用它们,了解python function。不要在 if statement 中定义函数。我认为你的代码应该是:

start = print('Answer yes or no to continue please\n') 
answer= input (str('Is this it\n'))

def f(): 
    print ('This could be it\n' )
def do_twice(answer):
    user_input = input (str('Are you sure?\n'))
    if user_input == answer:
        print('Ok thank you\n')
        print ('Your input is ', user_input)

if answer == 'yes': 
    f()
else:
    do_twice(answer)