Create a variable having in its name another variable (python)

Asked

Viewed 53 times

0

I’m doing a job in college that consists of analyzing data from covid-19 (through an excel spreadsheet). However one of the questions that my prof. asks is that with indication of a given week the program should provide for this week information X and as you know it has been more than 40 weeks since the beginning of the covid so if I keep creating subtypes for each week and for each column of approximately 170 lines of code only of subtypes, so I tried to give a variable having in its name another variable, unfortunately I did not succeed and I would like your help.

import xlrd
a=xlrd.open_workbook("covid.xlsx")
b=a.sheet_by_name("dados")
x=b.col_values(2)

i=int(input("Qual o valor de i?"))
if i>0 and i<5:
    _0imar=x[i+(7(i-1)):i+(7*i-(i-1))]
    print(_0imar)
if i>=5 and i<10:
    _0iabr=x[i+(7(i-1)):i+(7*i-(i-1))]
#Restante dos meses

<>:11: SyntaxWarning: 'int' object is not callable;perhaps you missed a comma?
<>:14: SyntaxWarning: 'int' object is not callable; perhaps you missed a comma?
<>:11: SyntaxWarning: 'int' object is not callable; perhaps you missed a comma?
<>:14: SyntaxWarning: 'int' object is not callable; perhaps you missed a comma?
Qual o valor de i?2
<ipython-input-65-dc2ab15c45d9>:11: SyntaxWarning: 'int' object is not callable;perhaps you missed a
comma?
      _0imar=x[i+(7(i-1)):i+(7*i-(i-1))]
    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-65-dc2ab15c45d9> in <module>
          9 i=int(input("Qual o valor de i?"))
         10 if i>0 and i<5:
    ---> 11     _0imar=x[i+(7(i-1)):i+(7*i-(i-1))]
         12     print(_0imar)
         13 if i>=5 and i<10:
    
TypeError: 'int' object is not callable

Thank you.

  • 1

    Pause your code, and study about "dictionaries" in Python - is what you need there: a single dictionary - not several dynamic variables.

  • The error you are having has nothing to do with the variable name - is why the operator "*" was missing for multiplication in the first part of the expression: [i+(7(i-1)):i+(7*i-(i-1))] . Reading error messages and understanding what they are saying is also key to getting anything done.

  • Because in fact after putting the "*" has already worked, a really stupid mistake and beginner, and also agree with the use of dictionary makes it simpler I ended up confusing myself due to the use of excel and I didn’t even end up remembering the dictionaries. Thanks for the help and the time

  • if you still want to use excel for other people to send you data, use openpyxl, for me was the best option of lib.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.