Jan-29-2018, 04:39 PM
Dear Python Users,
Sorry for bothering you with the posts. I am pretty new to programming. What I am trying to do is to promt user to enter 5 countries so that there are 5 indeces associated with those countries are downloaded. I am trying to make error handling, so that if the users enters a wrong country name, etc. the python asks him/her to re-enter country names. Thing is that it works one you enter all the countries wrong, but if you at least one country in a right way it prints that data of that country. However, what I want is that is ask the user to re-enter the contry names and dont print the downloaded data. Please, advise me on this issue.
Sorry for bothering you with the posts. I am pretty new to programming. What I am trying to do is to promt user to enter 5 countries so that there are 5 indeces associated with those countries are downloaded. I am trying to make error handling, so that if the users enters a wrong country name, etc. the python asks him/her to re-enter country names. Thing is that it works one you enter all the countries wrong, but if you at least one country in a right way it prints that data of that country. However, what I want is that is ask the user to re-enter the contry names and dont print the downloaded data. Please, advise me on this issue.
import quandl #To extract the data from Quandl website from quandl.errors.quandl_error import NotFoundError #for error handling from datetime import date, timedelta # to define the date and time, while "timedelta" - for 1 month back import matplotlib.pyplot as plt #Plotting library import pandas as pd #Time series working library from scipy import interpolate #Scipy library for interpolation import numpy as np def MainFormula(): exchange1, exchange2, exchange3, exchange4, exchange5 = [None]*5 while exchange1 is None or exchange2 is None or exchange3 is None or exchange4 is None or exchange5 is None: exchange1 = input('Please, choose a country from Germany, France, USA, HongKong, India, England, Japan or China: ') exchange2 = input('Please, choose a second country for comparison: ') exchange3 = input('Please, choose a third country for comparison: ') exchange4 = input('Please, choose a fourth country for comparison: ') exchange5 = input('Please, choose a fifth country for comparison: ') exchange1 = str(exchange1) #ensure that entered exchage is string type exchange2 = str(exchange2) #ensure that entered exchage is string type exchange3 = str(exchange3) #ensure that entered exchage is string type exchange4 = str(exchange4) #ensure that entered exchage is string type exchange5 = str(exchange5) #ensure that entered exchage is string type exchanges = [exchange1, exchange2, exchange3, exchange4, exchange5] date_time = date.today() #define today's date ten_years = date_time - timedelta(days=365*10) # define the date one month ago end = date_time.strftime("%Y/%m/%d") #make sure that date is in Y-m-d format start = ten_years.strftime("%Y/%m/%d") #make sure that date is in Y-m-d format try: if any(e.lower() == 'germany' for e in exchanges): global data_germany data_germany = quandl.get("CHRIS/EUREX_FDAX1", authtoken='hZcy4YazMK8shJxDxV-q', start_date=start, end_date=end) return data_germany else: print("Please, enter a coutry specified in the list") exchange1 = None if any(e.lower() == 'france' for e in exchanges): global data_france data_france = quandl.get("CHRIS/LIFFE_FCE1", authtoken='hZcy4YazMK8shJxDxV-q', start_date=start, end_date=end) return data_france else: print("Please, enter a coutry specified in the list") exchange2 = None if any(e.lower() == 'usa' for e in exchanges): global data_usa data_usa = quandl.get("MULTPL/SP500_REAL_PRICE_MONTH" , authtoken='hZcy4YazMK8shJxDxV-q', start_date=start, end_date=end) return data_usa else: print("Please, enter a coutry specified in the list") exchange3 = None if any(e.lower() == 'hongkong' for e in exchanges): global data_hongkong data_hongkong = quandl.get("CHRIS/HKEX_HSI1" , authtoken='hZcy4YazMK8shJxDxV-q', start_date=start, end_date=end) return data_hongkong else: print("Please, enter a coutry specified in the list") exchange4 = None if any(e.lower() == 'india' for e in exchanges): global data_india data_india = quandl.get("NSE/CNX_NIFTY" , authtoken='hZcy4YazMK8shJxDxV-q', start_date=start, end_date=end) return data_india else: print("Please, enter a coutry specified in the list") exchange5 = None if any(e.lower() == 'japan' for e in exchanges): global data_japan data_japan = quandl.get("NIKKEI/ALL_STOCK", authtoken='hZcy4YazMK8shJxDxV-q', start_date=start, end_date=end) return data_japan else: print("Please, enter a coutry specified in the list") exchange1 = None if any(e.lower() == 'england' for e in exchanges): global data_england data_england = quandl.get("CHRIS/LIFFE_Z1", authtoken='hZcy4YazMK8shJxDxV-q', start_date=start, end_date=end) return data_england else: print("Please, enter a coutry specified in the list") exchange2 = None if any(e.lower() == 'china' for e in exchanges): global data_china data_china = quandl.get("WFE/INDEXES_SHANGHAISESSECOMPOSITEINDEX",authtoken='hZcy4YazMK8shJxDxV-q', start_date=start, end_date=end) return data_china else: print("Please, enter a coutry specified in the list") exchange3 = None except ValueError as e1: print("Please, enter correct alpha (should be float - example: 0.2) ") exchange1 = None #reset ticker to continue looping return "Here are the results" program = MainFormula() print(program)