Home > Blockchain >  Problem with logic in python script
Problem with logic in python script

Time:01-18

#!/usr/bin/env python3
# tarchiver.py
# Purpose: Creates a tar archive of a directory
#
# USAGE: ./tarchiver.py
#
# Author:
# Date January 15th 2023
import os

correct_answer = 'yes'
correct_answer2 = 'no'
compression1 = 'gzip'
compression2 = 'bzip2'
compression3 = 'xzip'

print("Please enter the directory you would like to archive")
directory = input()
print("Please enter the name of the archive")
name = input()
print("Would you like your archive to be compressed?")
answer = input()
while correct_answer != answer or correct_answer2 != answer:
    answer = input()
    print('Please enter either yes or no')
    if answer == correct_answer or answer == correct_answer2:
        break
if answer == 'yes':
    print("What kind of compression do you want?")
    print("gzip, bzip2, or xzip?")
    answer2 = input()
    while compression1 != answer2 or compression2 != answer2 or compression3 != answer2:
        print('Please enter a valid answer')
        answer2 = input()
        if answer2 == compression1 or answer == compression2 or answer == compression3:
            break
    if answer2 == "gzip":
        os.system(f"tar -cvPzf {name} {directory}")
    if answer2 == "bzip2":
        os.system(f"tar -cvPjf {name} {directory}")
    if answer2 == "xzip":
        os.system(f"tar -cvPJf {name} {directory}")

I'm having trouble with the logic in the code. When it asks whether or not I would like compression and I type 'yes', I have to type it twice in order for the code to proceed to the next section. Also, when it asks for type and I input 'gzip', it tells me at first that it's an invalid input and that I need to correct my answer, but I just enter the same thing and then it proceeds to execute the rest of the code. This is for a school project and I'm new to python so excuse me if there is an obvious solution to this problem.

CodePudding user response:

while compression1 != answer2 or compression2 != answer2 or compression3 != answer2: answer2 can only equal at most one compression type, so it will be unequal to at least two. As such this line is equivalent to while True:

and you always and up having to enter the compression type until you break.

  • Related