查看新数据是否等于旧数据,如果旧数据等于新数据则不执行任何操作。否则将旧数据重命名为新数据

时间:2018-04-05 12:49:37

标签: python if-statement while-loop

由于我的错误标题描述我想检查我的新数据是否等于我的旧数据。 如果新数据等于旧数据:什么都不做。 如果新数据不等于旧数据:将旧数据更改为新数据。 然后使用旧数据的新定义重复整个过程。

制作此代码,但正如您所看到的,每次在最后重新定义旧数据时 在再次运行代码时再次重新定义。

def ask_data_change():
    data_inn = 'old data'    
    my_data_input = input('What is your input?: ')


    if data_inn == my_data_input:
        #does nothing
        print('Data input is equal to existing data')
        print('data unchanged: ' + data_inn)
    else:
        #Changes data_inn to new data input
        data_inn = my_data_input
        print('Data is not equal to existing, changing data_inn to my_data_input')
        print('New data is now: ' + data_inn)


while True:
    ask_data_change()

3 个答案:

答案 0 :(得分:0)

之所以发生这种情况,是因为您在每次调用时都重新定义data_inn,将while循环移动到函数本身并在外部重新定义旧数据:

def ask_data_change():
    data_inn = 'old data'
    while True:    
        my_data_input = input('What is your input?: ')

        if data_inn == my_data_input:
            #does nothing
            print('Data input is equal to existing data')
            print('data unchanged: ' + data_inn)
        else:
            #Changes data_inn to new data input
            data_inn = my_data_input
            print('Data is not equal to existing, changing data_inn to my_data_input')
            print('New data is now: ' + data_inn)

ask_data_change()

另一种选择是将data_inn作为全局变量,因此,它不会在函数中重新定义:

data_inn = 'old data'

def ask_data_change():
    global data_inn   
    my_data_input = input('What is your input?: ')
    if data_inn == my_data_input:
        #does nothing
        print('Data input is equal to existing data')
        print('data unchanged: ' + data_inn)
    else:
        #Changes data_inn to new data input
        data_inn = my_data_input
        print('Data is not equal to existing, changing data_inn to my_data_input')
        print('New data is now: ' + data_inn)

while True:
    ask_data_change()

答案 1 :(得分:0)

因为每次运行该函数时,都会重置所有值。这称为作用域(函数调用中定义的值保留在函数调用中,除非返回

见这里:

def f():
    i = 0
    i += 1
    print(i)

for _ in range(3):
    f()

输出:

1
1
1

正如您所看到的,每次都会在函数中重新定义i,它不会保留之前的值。您需要返回新值,然后在每次迭代时将其传递给函数,如下所示:

def ask_data_change(data_inn):
    my_data_input = input('What is your input?: ')  

    if data_inn == my_data_input:
        #does nothing
        print('Data input is equal to existing data')
        print('data unchanged: ' + data_inn)
    else:
        #Changes data_inn to new data input
        data_inn = my_data_input
        print('Data is not equal to existing, changing data_inn to my_data_input')
        print('New data is now: ' + data_inn)

    return data_inn


data_inn = 'old data'
while True:
     data_inn = ask_data_change(data_inn)

答案 2 :(得分:0)

每次在外部函数中调用ask_data_change()时,您都会重置两个值

假设您想在键入不同输入时停止循环,请尝试使用

def ask_data_change():
    data_inn = 'old data' 
    my_data_input = input('What is your input?: ')

    while data_inn == my_data_input:
        #does nothing
        print('Data input is equal to existing data')
        print('data unchanged: ' + data_inn)
        my_data_input = input('What is your input?: ')

    #Changes data_inn to new data input
    data_inn = my_data_input
    print('Data is not equal to existing, changing data_inn to my_data_input')
    print('New data is now: ' + data_inn)
    return data_inn # Assuming you want this value 

data_inn = ask_data_change()