Python 3 int() function is not converting input string to integer

前端 未结 1 2002
一生所求
一生所求 2021-01-23 11:42

I am working on a small program that reads data from a CSV file. As part of the program, user input is used to only select data that >= but I get TypeError: unorderable

相关标签:
1条回答
  • 2021-01-23 12:14

    int does not cast its parameters to integer in-place. In fact those parameters are immutable.

    int(sessions) does not exactly do what you think it does. session is not modified, but the return value of that call is an int.

    You should assign the returned value to a new/same name:

    sessions = int(sessions)
    pageviews = int(pageviews)
    

    The operator >= can now compare the two variables you have, since they are now both integers.


    You may also want to rewrite that if block like so:

    if data_type == 'sessions':
         for page, sessions in ga_session_data.items():
             if sessions >= int(num):
                 print(page, ' - ', sessions)
    

    In this way, you're actually checking the sessions count in the dictionary and not the sessions from the for loop.

    0 讨论(0)
提交回复
热议问题