Home > database >  in a sequence, how to compare two added variables?
in a sequence, how to compare two added variables?

Time:11-04

the problem is more specific. in a given list sequence. the numbers I want to compare are a variable(let's call it current varaible) and the variable before the current variable (let's call it before varaible). which means if the current variable's index is 1, then the before variable is 0. but I want that the index of the current variable will be the last variable in the list. additionaly, the "current variable" is given by a user input. the input is being ran in a loop so that there are constantly new "last variables in the list". thus I always need to compare new variables. eventually, after having the ability to compare those variables, whaat I want to compare in them is their value. If their value is equal then the program does something, ELSE the program continues as normal.

the problem here, is that I am struggling with how to compare such variables. although I am not struggling with comparing their values.

I wrote two different scripts, while trying to solve the problem mentioned above, in both scripts I encountered a different problem.

the first:

    list_= []
    n = 0
    loop = True
    while(loop == True):
     current_variable = input("what is your current variable?")
     if(list_[n]==list_[n 1]):
       print("the two values are equal")
     else:
      list_.append(current_variable)
      n = n 1

here I got an error that "list index out of range". I understood the problem and created a new script: on the list I added variables. the only change to the script was that:

       list_= ["solving the problem", "again solving the problem"]

here I got a problem, that for some reason the message "the two values are equal" was printed only after the third input, or when the input was "again solving the problem" only after the second input. why the third??

CodePudding user response:

There are so many things wrong with your program its difficult to know where to start.

You don't need an index n if you are only adding things to the end of a list or dealing with the last element of a list.

Your while can be while True: since you never alter loop.

You are trying to compare the last two items in a list: list_[n]==list_[n 1], but you ignore the new item entered by the user. You should be comparing current_variable against the end of the list.

Also you don't do anything with the list, so my program just prints the list so you can tell what its doing.

list_ = []
while True:
     print(list_)
     current_variable = input("what is your current variable?")
     if not list_ or list_[-1] != current_variable:
         list_.append(current_variable)
         continue
     print("the two values are equal")

The line: if not list_ or list_[-1] != current_variable: combines two steps in one. The not list_ checks to see if the list is empty. If it is you are allowed to add the current_variable onto the list without checks. Otherwise it compares the last item with current_variable and if they are different you can add it to list_.

  • Related