i need help to shorten this code a lot preferable id like to make the month selection a function but cant due to having to call in different csv files

Question:

i need help to shorten this code a lot

im trying to make the month selection witch is repeating shorter or into a function preferable but cant make it work, id like to make the month selection a function but cant due to having to call in different csv files what you guys got?
csv files (https://send.tresorit.com/a#1NoM7CSW08PLTTNbtHMcHw)
password:"csvtest"

import pandas as pd
def year():
    print("Type '2018' to select the data of 2018")
    print("Type '2019' to select the data of 2019")
    print("Type '2020' to select the data of 2020")
    print("Type '0' to close selection")

def select_the_month_of_Etherium():
    year()
    while True:
        b=int(input("Select the year:"))
        if b == 2018:
            df8 = pd.read_csv("C:\Users\seena\OneDrive\Desktop\2022-11-18 20.55.00\Project csv ETHERIUM2018"
                              ".csv")#importing a csv file
            a = int(input("Enter the month(Number Only):"))
            print("Type '0' to close selection")
            if a == 1:
                c = df8.loc[0]
                print(c)
            elif a == 2:
                c = df8.loc[1]
                print(c)
            elif a == 3:
                c = df8.loc[2]
                print(c)
            elif a == 4:
                c = df8.loc[3]
                print(c)
            elif a == 5:
                c = df8.loc[4]
                print(c)
            elif a == 6:
                c = df8.loc[5]
                print(c)
            elif a == 7:
                c = df8.loc[6]
                print(c)
            elif a == 8:
                c = df8.loc[7]
                print(c)
            elif a == 9:
                c = df8.loc[8]
                print(c)
            elif a == 10:
                c = df8.loc[9]
                print(c)
            elif a == 11:
                c = df8.loc[10]
                print(c)
            elif a == 12:
                c = df8.loc[11]
                print(c)
            else:
                print("Invalid choice")
        elif b == 2019:
            df8 = pd.read_csv("C:\Users\seena\OneDrive\Desktop\2022-11-18 20.55.00\Project csv ETHERIUM Y(2).csv")#importing a csv file
            a = int(input("Enter the month(Number Only):"))
            print("Type '0' to close selection")
            if a == 1:
                c = df8.loc[0]
                print(c)
            elif a == 2:
                c = df8.loc[1]
                print(c)
            elif a == 3:
                c = df8.loc[2]
                print(c)
            elif a == 4:
                c = df8.loc[3]
                print(c)
            elif a == 5:
                c = df8.loc[4]
                print(c)
            elif a == 6:
                c = df8.loc[5]
                print(c)
            elif a == 7:
                c = df8.loc[6]
                print(c)
            elif a == 8:
                c = df8.loc[7]
                print(c)
            elif a == 9:
                c = df8.loc[8]
                print(c)
            elif a == 10:
                c = df8.loc[9]
                print(c)
            elif a == 11:
                c = df8.loc[10]
                print(c)
            elif a == 12:
                c = df8.loc[11]
                print(c)
            else:
                print("Invalid choice")
        elif b == 2020:
            df8 = pd.read_csv("C:\Users\seena\OneDrive\Desktop\2022-11-18 20.55.00\Project csv ETHERIUM Y(3).csv")#importing a csv file
            a = int(input("Enter the month(Number Only):"))
            print("Type '0' to close selection")
            if a == 1:
                c = df8.loc[0]
                print(c)
            elif a == 2:
                c = df8.loc[1]
                print(c)
            elif a == 3:
                c = df8.loc[2]
                print(c)
            elif a == 4:
                c = df8.loc[3]
                print(c)
            elif a == 5:
                c = df8.loc[4]
                print(c)
            elif a == 6:
                c = df8.loc[5]
                print(c)
            elif a == 7:
                c = df8.loc[6]
                print(c)
            elif a == 8:
                c = df8.loc[7]
                print(c)
            elif a == 9:
                c = df8.loc[8]
                print(c)
            elif a == 10:
                c = df8.loc[9]
                print(c)
            elif a == 11:
                c = df8.loc[10]
                print(c)
            elif a == 12:
                c = df8.loc[11]
                print(c)
            else:
                print("Invalid choice")
        elif b == 0:
            break
        else :
            print("Invalid choice")

select_the_month_of_Etherium()
Asked By: Athul Tomy

||

Answers:

You can do:

if a in range(1,13):
    c = df8.loc[a-1]
    print(c)
else:
    print("Invalid choice")
Answered By: Joles

Look at the pattern of your if statements. The value of c is set to an element located at a certain point in df8. All of your if statements check a and then find the location of the element using the value a minus 1 as the index.

Meaning that all your if statements (the ones that check if a == 1 to 12) can be summed to:

if a <= 0 or a > 12: # checks for 0 and less or greater than 12 since there aren't more than 12 months in a year
    break # output error or do what you would like
else:
    c = df8.loc[a-1]
Answered By: Trooper Z
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.