1

I have the CSV file:

                                  Movie     Budget  Worldwide Gross  Year
0                                  2012  200000000        791217826  2009
1                               Aladdin  183000000       1050693953  2019
2                   Alice in Wonderland  200000000       1025468216  2010
3                               Aquaman  160000000       1148485886  2018
4                                Avatar  237000000       2847246203  2009 

and I have the following code:

opcion = datos[datos["Year"] == 2019 ]

this helped me just show the data from that year...

but my problem is that the year (in this case 2019) needs to be provided by the user (or any other year that they would want to look for) and I dont know how to use int(input()) in order to do that.

thank you

4
  • opcion = datos[datos["Year"] == int(input())] or replace the number 2019 with a variable that you read using int(input()) on a separate line. Commented May 21, 2021 at 20:23
  • thank you! I get this error: Traceback (most recent call last): File "<pyshell>", line 1, in <module> ValueError: invalid literal for int() with base 10: '' i'll try the other way :) Commented May 21, 2021 at 20:30
  • The input still has to be a valid number. Commented May 21, 2021 at 20:31
  • 1
    It worked! I used usuario = int(input('cual es el a? ')) opcion = datos[datos["Year"] == usuario ] opcion... Thank you very much!! Commented May 21, 2021 at 20:36

1 Answer 1

1

A snippet of code to catch exception

try:
    year = input("Enter a year: ")
    opcion = datos[datos["Year"] == int(year)]
    if opcion.empty:
        print(f"No result for year '{year}'")
except ValueError:
    print(f"'{year}' is not a valid date")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.