mercredi 12 mai 2021

IF statements not recognising user input [duplicate]

Rookie question but I've googled around and I can't figure out why this isn't working.

I have a script that makes archive copies of files for me. That script works fine but I'm trying to build an extra part that allows me to choose whether to archive just one or both sets of files when I run it. In theory this would be Python 101 but I can't get it to work:

scriptResp = input("Do you want to archive scripts? (Scripts: 1, XML: 2, Both:3)")


print(scriptResp)

if scriptResp != 1 or 2 or 3:
    print("Invalid response, script archiving will be skipped.")
    scriptResp = 0
    
    
if scriptResp == 1 or 3:
    print("Running script backup...")
    print("Backup done.")
    
if scriptResp == 2 or 3:
    print("Running XML backup...")
    print("Backup done.")
    
if scriptResp == 0:
    print("No backups done today.")

I've not included the file archive code as it works fine and it's not relevant. When I run this, I enter 1 as my input but it still prints the 'invalid response' text. It then moves to the second if and runs that, which would make sense, but then also runs the second one:

Do you want to archive scripts? (Scripts: 1, XML: 2, Both:3)1
1
Invalid response, script archiving will be skipped.
Running script backup...
Backup done.
Running XML backup...
Backup done.
No backups done today.

It's like my IF statements are reading any input as False and just running everything. Is this an issue with my use of the 'or' operator in my IF statements? Again, apologies for the rookie question but according to what I've found on Google this should work!

Aucun commentaire:

Enregistrer un commentaire