vendredi 9 juin 2017

Python if condition always evaluates to false regardless of user input

Following is my code creating an HTTP or FTP connection depending on user input. The if and elif conditions somehow evaluate to FALSE all the time. Entering 1 and 0 both prints 'Sorry, wrong answer'.

domain = 'ftp.freebsd.org'
path = '/pub/FreeBSD/'

protocol = input('Connecting to {}. Which Protocol to use? (0-http, 1-ftp): '.format(domain))
print(protocol)
input()

if protocol == 0:
    is_secure = bool(input('Should we use secure connection? (1-yes, 0-no): '))
    factory = HTTPFactory(is_secure)
elif protocol == 1:
    is_secure = False
    factory = FTPFactory(is_secure)
else:
    print('Sorry, wrong answer')
    import sys
    sys.exit(1)

connector = Connector(factory)

try:
    content = connector.read(domain, path)
except URLError as e:
    print('Can not access resource with this method')
else:
    print(connector.parse(content))

Output:

Connecting to ftp.freebsd.org. Which Protocol to use? (0-http, 1-ftp): 0
0

Sorry, wrong answer
$ python abstractfactory.py
Connecting to ftp.freebsd.org. Which Protocol to use? (0-http, 1-ftp): http
http

Sorry, wrong answer
$ python abstractfactory.py
Connecting to ftp.freebsd.org. Which Protocol to use? (0-http, 1-ftp): 1
1

Sorry, wrong answer

$

Please advice. What am I doing wrong here? Thanks.

Aucun commentaire:

Enregistrer un commentaire