Voici mon code créant une connexion HTTP ou FTP en fonction de l'entrée de l'utilisateur. Les conditions if et elif évaluent en quelque sorte à FAUX tout le temps. Si vous entrez 1 et 0, les deux impressions "Désolé, mauvaise réponse".Python si la condition est toujours évaluée à false indépendamment de l'entrée de l'utilisateur
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))
Sortie:
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 $
conseils S'il vous plaît. Qu'est-ce que je fais mal ici? Merci.