Estoy buscando usar un bucle for para crear múltiples variables, nombradas en la iteración (i), y asignar a cada una un int único.
Xpoly = int(input("How many terms are in the equation?")) terms={} for i in range(0, Xpoly): terms["Term{0}".format(i)]="PH" VarsN = int(len(terms)) for i in range(VarsN): v = str(i) Temp = "T" + v Var = int(input("Enter the coefficient for variable")) Temp = int(Var)
Como veis al final me perdí. Idealmente estoy buscando una salida donde
T0 = # T1 = # T... = # T(Xpoly) = #
¿Alguna sugerencia?
Puedes hacer todo en un bucle
how_many = int(input("How many terms are in the equation?")) terms = {} for i in range(how_many): var = int(input("Enter the coefficient for variable")) terms["T{}".format(i)] = var
Y luego puedes usar
print( terms['T0'] )
Pero probablemente sea mejor usar una lista en lugar de un diccionario.
how_many = int(input("How many terms are in the equation?")) terms = [] # empty list for i in range(how_many): var = int(input("Enter the coefficient for variable")) terms.append(var)
Y luego puedes usar
print( terms[0] )
o incluso (para obtener los tres primeros términos)
print( terms[0:3] )