Python Forum
Issue with error handling
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Issue with error handling
#1
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.

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)  
Reply
#2
Well, I am not new to Python, actually I am newer than you. I am just trying to help so excuse me if my help is not what you are expecting.

I see that you have 8 countries available for the user, and the user has to use only 5 countries from the list. I cannot understand what the else section is doing.
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
What I see, that someone would choose any combination from the 8 countries, or would even enter invalid country. So, why not to check using the if statement whether the countries in [exchanges] is belonging to the original preferred 8 countries?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  error handling in multiprocessing /spawn.py Cees 0 2,866 Feb-03-2020, 03:22 PM
Last Post: Cees

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020