-1

I got to write a script which replaces certain things in a .txt file with a specific variables from input() and then it should create a new .txt with exact same content except the things replaced.

I have a few conditions:

Which compares line content from original .txt with a specific strings and if it's equal, it then replaces the string with replaced one and writes this new string to a new .txt file.

Basically, I must copy\paste original file with a few touches. But when it comes to the test stage, the whole config would be rewritten with only first condition stated in the build_config() function. And If you run my script, you would see that the LACP condition is not working at all, it writes the content in whichever way, when it should only write it if the LACP variable equals to "y".

What's going on? Here is my code:

def build_config():
    base_config = open("MES2428B.txt", 'r')
    for line in base_config:
        complete_config = open(location + ".txt", 'a')
        complete_config.write(line)
        line.replace("location", "location"+location)
    complete_config.close()
    base_config.close()
    if lacp == "y" or "Y" or "Н" or "н" :
        base_config = open("MES2428B_lacp.txt", 'r')
        for line in base_config:
            complete_config = open(location + ".txt", 'a')
            complete_config.write(line)
        base_config.close()
        complete_config.close()

print("LOCATION:")
location = input()
print("VLAN:")
vlan = input()
print("Adress:")
ip = input()
print("LACP NEEDED? y/n")
lacp = input()
print("COM SPEED:")
COMspeed = input()

print("LOCATION: " + location + "\nVLAN: " + vlan + "\nIP: " + ip)

print("IS EVERYTHING RIGHT? y/n")

while True:
    check = input()
    if check == "y" or "Y" or "Н" or "н":
        build_config()
        print("Done, check it!")
        input()
        break
    elif check == "n" or "N" or "т" or "Т": 
        print("What would you like to change? /n 1 - Локация /n 2 - VLAN /n 3 - IP /n 4 - COM Скорость /n 5 - nothing")
        check = input()
        if check == 1:
            location = input()
        elif check == 2:
            vlan = input()
        elif check == 3:
            ip = input()
        elif check == 4:
            COMspeed = input()
        elif check == 5:
            print('Then why you press it!?')
            build_config() 
            print("Done! Check it!")
            input()
            break
    elif True:
        print("Your input is garbage, try again") 
'''

martineau
  • 119,623
  • 25
  • 170
  • 301
reeqo
  • 7
  • 1

2 Answers2

0

Welcome to SO.

Where you have if check == "y" or "Y" or "Н" or "н":, you want if check.lower() in ('y', 'h'):

Pedro Rodrigues
  • 2,520
  • 2
  • 27
  • 26
0

The or operator takes two boolean values and returns a boolean value.

This expression:

lacp == 'y' or 'Y'

would be evaluated as follows:

  • Evaluate lacp == 'y'.
  • If it is true, the whole expression returns true.
  • Otherwise, evaluate 'Y' as a boolean value and return that. Any non-zero, non-empty value is coerced to true. So your expression returns true.

The proper general way to incorporate multiple comparisons would be:

(lacp == 'y') or (lacp == 'Y')

The parentheses should not be necessary here, but I think it is a good habit to use them to make the order of evaluation clear.

For specific cases such as yours, there might be other reasonable ways to do your test, as shown in another answer.

Dave Costa
  • 47,262
  • 8
  • 56
  • 72